If you dynamically allocated foo , for example:
foo* f = new foo;
then delete f destroy the dynamically allocated object foo , including the pointers that it contains, but does not point to pointers if they really point to dynamically allocated objects or arrays of objects.
If you assigned a dynamically allocated foo object (i.e. the result of new foo ) to shared_ptr (assuming tr1 or boost), then when the last shared_ptr referencing this object goes beyond delete will be called by the pointer originally returned by new foo automatically . You do not need to do this manually.
If your object ( foo ) contains pointers to dynamically allocated objects that it owns (so you need to free it at the end of the foo life cycle), then it is highly recommended to write a destructor to free these objects (which will depend on how they are distributed).
After you write the destructor, you need to think about whether you need to write a copy constructor and a copy operator. When you use a vector of generic pointers, you can decide that your objects should not be copied. If so, you can declare them closed and not need to be implemented.
You should also consider one or more constructors to make sure your pointer elements are initialized. If the pointer elements are never initialized, then if they are not assigned during the life of foo , then they will be neither null nor exact for the actual object, and this may cause an error in your destructor.
Charles Bailey
source share