If you are using the C ++ 11 compiler, just use std :: shared_ptr and you do not need to worry about deleting. This is because shared_ptr is a smart pointer that automatically deletes what it points to.
Use a generic pointer whenever you highlight something:
#include<memory> ... { .... std::shared_ptr<B> ptrB( new B(bb1, aa1) ); //Here is another, more readable way of doing the same thing: //auto ptrB = std::make_shared<B>(bb1,aa1); ... } //no memory leaks here, because B is automatically destroyed
Here is more information on the topic of smart pointers.
I should also note that if you don't have a C ++ 11 compiler, you can get generic pointers from the BOOST library .
source share