In article <1163195636.132623.134380@e3g2000cwe.googlegroups. com>,
"Karim" <karim.elsaid@gmail.comwrote:
Quote:
Hi,
>
There is a certain structure that I need and I am planning to implement
but I wanted to make sure nothing similar that already exists.
>
My main objective is to have a vector that mainly carries pointers and
in it destructor, it would delete those pointers.
>
so it would be auto_vector<Class1 * autoVec;
autoVec.push_back(new Class1());
>
and somewhere in ~auto_vec there would be a delete for all memebers.
>
My question is, is there anything in STL that already does this
funtionality? and does anyone see any potential issues with this.
|
* There is:
std::vector<std::tr1::shared_ptr<T
which isn't exactly what you want, but it is somewhat close. Substitute
boost::shared_ptr for std::tr1::shared_ptr if you don't have the tr1
version.
http://www.boost.org/libs/smart_ptr/shared_ptr.htm
* There is the boost Pointer Container Library which sounds very close
to what you want:
http://www.boost.org/libs/ptr_contai...container.html
* Looking to the future, for C++0X I believe there will be:
std::vector<std::unique_ptr<T>>
which is roughly equivalent to vector<auto_ptr<T>except that it works.
http://www.open-std.org/jtc1/sc22/wg...56.html#Additi
on%20-%20Class%20template%20unqiue_ptr
If you still want to write your own, the issues are:
* What are the copy semantics? I.e. what happens when you copy
construct or assign one of these things.
* Will you try to emulate move semantics (which is awkward, but mostly
doable in today's C++)? Move semantics essentially means transferring
ownership of the pointers.
For example, auto_ptr has no copy semantics. It does have move
semantics, but uses copy syntax to accomplish the move semantics (which
is why it is fundamentally unsafe).
-Howard