[英]How to use use smart pointers with Allegro Bitmaps?
I've decided I'm tired of deciding which classes are responsible for deleting which bitmaps.我已经决定我厌倦了决定哪些类负责删除哪些位图。 I've tried to rewrite my code to use smart pointers with a custom deleter
al_destroy_bitmap
我试图重写我的代码以使用带有自定义删除器
al_destroy_bitmap
智能指针
My code is pretty simple.我的代码很简单。
shared_ptr<ALLEGRO_BITMAP> test = make_shared<ALLEGRO_BITMAP>(al_load_bitmap("hello.png"), al_destroy_bitmap);
I'm getting quite a few errors which I just can't seem to work around.我遇到了很多我似乎无法解决的错误。
error C2079: 'std::_Get_align<ALLEGRO_BITMAP>::Elt2' uses undefined struct 'ALLEGRO_BITMAP'
error C2079: 'std::_Get_align<ALLEGRO_BITMAP>::Elt0' uses undefined struct 'ALLEGRO_BITMAP'
error C2027: use of undefined type 'ALLEGRO_BITMAP'
error C2027: use of undefined type 'ALLEGRO_BITMAP'
Other solutions to solve my problem would be creating a Bitmap class to wrap all of the Allegro stuff, but that seems ugly, and I don't think I should have to do that.解决我的问题的其他解决方案是创建一个 Bitmap 类来包装所有 Allegro 的东西,但这看起来很难看,我认为我不应该这样做。 Plus I already use other allegro functions everywhere, and then I would want to then write those same types of classes for
ALLEGRO_SAMPLE
and ALLEGRO_FONT
.另外,我已经在任何地方使用了其他快板函数,然后我想为
ALLEGRO_SAMPLE
和ALLEGRO_FONT
编写那些相同类型的类。 I really don't feel like doing that.我真的不想那样做。
How can I use smart pointers with Allegro bitmaps?如何在 Allegro 位图上使用智能指针?
Edit: Perhaps to give non-Allegro coders a feel for how ALLEGRO_BITMAPs work, I'll post a little code below编辑:也许是为了让非 Allegro 编码人员了解 ALLEGRO_BITMAPs 的工作方式,我将在下面发布一些代码
ALLEGRO_BITMAP *test = al_load_bitmap("hello.png") // This works
ALLEGRO_BITMAP test1; // This fails with error C2079 undefined struct ALLEGRO_BITMAP. I expect this here.
Like this:像这样:
std::shared_ptr<ALLEGRO_BITMAP> test(al_load_bitmap("hello.png"), al_destroy_bitmap);
Remember: make_shared
and make_unique
call new
, you don't want that here.记住:
make_shared
和make_unique
调用new
,你不想在这里。
Also make_x<T>
constructs an object of type T
, passing the given arguments to T
's constructor.同样
make_x<T>
构造一个类型T
的对象,将给定的参数传递给T
的构造函数。 But al_load_bitmap
returns a pointer to a fully constructed object so you don't want to call any constructor (except the smart pointer constructor).但是
al_load_bitmap
返回一个指向完全构造对象的指针,因此您不想调用任何构造函数(智能指针构造函数除外)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.