Why not?
Maybe you want a vector of mp_images or a map?
But there's so many more containers in the standard, too, of which all would be broken by auto_ptr.
>> What do you think of that idea?
Still dangerous. Trying to roll your own solution is still going to leave you with problems.
What about shared_array?
What I relized about shared_array is that it will always remove the pixel data after all mp_images using the data have been removed. My class has been given a constructor to create an image from already existing pixel data (such as from a SDL_Surface), meaning that not all mp_images do really own the pixel data, but they still have r/w access to it. Which means that some mp_images do not have permission to remove the pixel data when destroyed (for which I have a flag to tell), but would do it anyway if I use shared_array. I have thought of implementing a simple counter instead of a shared_array, though, and let the mp_image destructor decide when to delete the data or not.
Last edited by TriKri; 08-17-2008 at 04:23 AM.
Come on, you can do it! b( ~_')
>> meaning that not all mp_images do really own the pixel data
If the mp_image doesn't own the data, then don't have it use new or delete at all and let whatever owns it take care of the memory management. All you have to do is make sure all the instances of your class don't try to use the image once you destroy it. Otherwise that makes it really easy, just store a pointer to the data.
It has other constructors as well, which allocates memory for new pixel data. In that case the object does become responsible for removal of the data.
Just a question, what feels the most natural to you; assignment which copies the pointer and not the data (this would be the same as using memset on the two objects), or assignment which copies the data so you actually have two different but identical images?
Come on, you can do it! b( ~_')
>> what feels the most natural to you
They are equal, it depends on your situation.
Are there any scenarios in which both copies will be active at the same time? If not, then copying the data is not a good solution.
If there are those scenarios, then the question is if one object modifies the data should that change be reflected in the other object's data, or should the other object's data be unaffected. If the other object should be unaffected, then you need to copy the data. If the other object should be updated, then you need to share the data.
If you can answer those questions, then you'll be closer to what you need. If the answers are different in different situations, then you might want to re-think your design and make it all work the same or come up with different objects for different scenarios.