intmain(){ Base* b = newBase(); delete b; std::cout << "----------\n"; Derived* d = newDerived(); delete d; }
output:
1 2 3 4 5 6 7
Base constructor called Base deconstructor called ---------- Base constructor called Derived constructor called Derived deconstructor called Base deconstructor called
When we define a Base type pointer and the pointer points to Derived, things are different. The ~Derived() is not called.
Base constructor called Derived constructor called Base deconstructor called
This is when we might have memory leak in C++. Suppose we have data created in Derived, the destructor is responsible for cleaning up the memeory, however, it never gets called.