John <>, on 24/06/2010 21:41:52, wrote:
>>> Is there an easier/smarter way to the same thing to avoid destructors?
>>> e.g.
>>> using std::vector?
>> Yes, you just need to use some kind of smart pointer, make a search and
>> you'll find plenty of options.
>
> can you show an short example how would you do the above with smart pointer
> (preferably std::vector)? wrapping smart pointer in a class constructor is
> kind-of abstract to me...
>
Well, nothing really difficult, you would just replace your array with a
vector, then in the constructor of you container (as well as somewhere
else, if needed) you would push smart pointers into the vector, maybe
within a loop, just like when you have looped your array to assign the
raw pointers with the addresses of the newly created objects:
vec.push_back(smart_pointer(new obj));
where vec is a std::vector, obj is the name of the class you want to
store pointers of, and smart_pointer is the constructor of the smart
pointer you decide to use - and that's the truly important point: the
smart pointer that you choose.
Make a search for "std::auto_ptr with std::vector" and you will see what
I mean.
--
FSC
http://userscripts.org/scripts/show/59948