In class A there is no reason for the .clear() member variable vector -type in the destructor. The vector destructor will be .clear() vector when it is called.
In class B the cleanup code can simply be written as:
delete p_;
There is no need to check if p_ != NULL first, because delete NULL; defined as no-op. There is also no need to set p_ = NULL after you delete d, because p_ can no longer be legally accessible after the destruction of the object of which it is a member.
However, you rarely need to use delete in C ++ code. You should prefer to use visibility-limited resource management (SBRM, also called resource acquisition initialization) to automatically manage resource resource timing.
In this case, you can use the smart pointer. boost::scoped_ptr and std::unique_ptr (from C ++ 0x) are a good choice. None of them should have an overhead compared to using an unprocessed pointer. In addition, they both suppress the generation of the implicitly declared copy constructor and copy assignment operator, which you usually want when you have a member variable that is a pointer to a dynamically allocated object.
James McNellis
source share