端口结构到类
我正在使用 Allegro 5 框架。当我需要创建事件队列时,我必须调用“al_create_event_queue”并检查错误,并销毁它“al_destroy_event_queue”。由于我必须使用相同的机制来创建每个对象,因此非常无聊。
我的问题是:有一种方法可以将结构“移植”到类中,以便 my_event_queue 的构造函数实际上调用“al_create_event_queue”并且析构函数调用“al_destroy_event_queue”? 如果没有,我如何跟踪这些函数创建的对象,以便在我的“游戏”主处理程序类被破坏时自动删除它们?
I'm using the Allegro 5 framework. When I need to create an event queue I have to call 'al_create_event_queue' and check for errors, and to destroy it 'al_destroy_event_queue'. Since it is the same mechanism I have to use for each object to be created, it is quite boring.
My question is: there is a way to 'port' a structure to a class so that the constructor of my_event_queue actually calls the 'al_create_event_queue' and the destructor calls the 'al_destroy_event_queue'?
If not, how could I track object created by these functions so that they are auto-deleted when my 'Game' main handler class is destructed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。你完全按照你说的去做。你似乎已经自己解决了这个问题。
但是,您需要确保正确处理复印。你应该要么禁止复制这个对象(通过类似 boost::noncopyable 的方式),要么你应该为它编写一个复制构造函数和复制赋值运算符。现在,Allegro 事件队列不可复制(没有 Allegro 功能),因此您可能应该禁止复制。
如果您可以访问 C++0x,move 构造函数和移动赋值运算符就可以了。
Yes. You do exactly what you said. You seem to have figured this one out for your self.
However, you need to make sure you handle copying correctly. You should either disallow copying of this object (via something like boost::noncopyable), or you should write a copy constructor and copy assignment operator for it. Now, Allegro event queue's are not copyable (there's no Allegro function for that), so you should probably just disallow copying.
If you have access to C++0x, a move constructor and move assignment operator would be fine.
当然,您可以...只需将创建结构的代码放在构造函数中,并将删除它的代码放在析构函数中。
请注意,您不能做太多的事情来包装这些类型...您在 Allegro 代码中传递了太多的指针,以至于您基本上必须向世界公开底层的队列对象。
Of course you can... simply put the code to create the structure in the constructor, and the code to delete it in the destructor.
Note that you can't do too much to wrap these types... you pass around those pointers so much in Allegro code that you basically have to expose the underlying
queue
object to the world.