Prev: Constant Time std::vector Item Removal
Next: Distinguish between pointers created with 'new' and created with references.
From: Francis Glassborow on 15 Jul 2010 08:47 dailos wrote: > Hi all, > I am struggling with a design problem. It showed up because of some > architectures constraints. > I have a group of classes which derive from an abstract one. > I have a vector with pointers to the generic abstract class (neither > vector of references nor vector of classes are allowed since it's > abstract). And there are far better reasons than that for not allowing that. A vector needs to be homogeneous because its design requires a (dynamic) array. > A member function that loads objects into the vector, lets say from > file, has to do it creating object with new operator since it couldn't > be done by references to objects created locally. > A public functions lets the user to add single objects too but it > could use new or reference as an argument. > The problem is when I need to call the destructor, and erase all of > the objects created within the vector. > I thought of using delete operator for each element in the vector, but > what if some other elements has been added from a reference. delete > &something ## failure. > > I guess there is no way to distinguish between object address created > with new and created with a reference. > How a to write a proper destructor then?? > > Here it is a very easy program that shows the situation. > Thanks!! > The above is an excellent argument for not doing that. Your collection should be of objects formed the same way and if it is a collection of pointers the pointed too objects must be created the same way. I guess you could create some kind of smart pointer that tracks whether the pointer owns the object or not but I think the resulting code would be too fragile for general use. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Seungbeom Kim on 16 Jul 2010 01:19
On 2010-07-15 05:03, dailos wrote: > The problem is when I need to call the destructor, and erase all of > the objects created within the vector. > I thought of using delete operator for each element in the vector, but > what if some other elements has been added from a reference. delete > &something ## failure. > > I guess there is no way to distinguish between object address created > with new and created with a reference. > How a to write a proper destructor then?? This is an illustration of why a vector of raw pointers is not a good idea. It is not robust, to begin with: if you remove an element from the vector or overwrite an element in the vector or simply call .clear() on the vector, you get a memory leak! A robust solution is to make each element take care of itself, as in RAII. A vector of smart pointers, such as vector< shared_ptr<T> > (where shared_ptr comes from Boost or TR1 or C++0x), does the job. For your problem, you can simply use a shared_ptr with a no-op deleter. -- Seungbeom Kim [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |