If you have dynamically allocated foo
, e.g.:
foo* f = new foo;
then delete f
will destroy the dynamically allocated foo
object including the pointers it contains but not anything pointed to by the pointers, if they do indeed point at dynamically allocated objects or arrays of objects.
If you've assigned a dynamically allocated foo
object (i.e. the result of new foo
) to a shared_ptr
(assuming tr1 or boost) then when the last shared_ptr
referring to that object goes out of scope delete
will be called on the pointer originally returned by new foo
automatically. You don't have to do this manually.
If your object (foo
) contains pointers to dynamically allocated objects that it owns (so need deallocating at the end of the lifetime of the foo
), then it's highly recommended that you write a destructor to deallocate these objects in the correct way (which will depend on how they are allocated).
Once you've written a destructor you need to consider whether you need to write a copy constructor and copy-assingment operator. As you are using a vector of shared pointers you may decide that your objects should not be copied. If so you can declare these as private and need not provide an implementation.
You should also consider one or more constructors to ensure that your pointer members are initialized. If the pointer members are never initialized then if they are not assigned to during the lifetime of a foo
then they will neither be null, nor point to a valid object and this is likely to cause an error in your destructor.