开发者

Port structure to class

开发者 https://www.devze.com 2023-03-15 23:36 出处:网络
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 sa

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 tha开发者_StackOverflowt 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?


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.


Of course you can... simply put the code to create the structure in the constructor, and the code to delete it in the destructor.

struct MyQueue {
  MyQueue() : queue(al_create_event_queue() { }
  ~MyQueue() { al_destroy_event_queue(queue); }

  ALLEGRO_EVENT_QUEUE* queue;

private:
  MyQueue(const MyQueue&);
  MyQueue& operator =(MyQueue);
};

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号