inheritance - Why can't class be destructed in polymorphism C++ -


i have problem using polymorphism. code here:

class { public:     a() {cout << "construct a" << endl;}     virtual ~a() {cout << "destroy a" << endl;} };  class b : public a{ public:     b() {cout << "construct b" << endl;}     ~b() {cout << "destroy b" << endl;} }; void main() {     *p = new b; } 

the result is:

construct construct b 

why 'p' can not destroyed or may wrong somewhere. thanks.

you need invoke

delete p; 

to delete pointer , call destructor. dynamically allocated objects not have destructor called automatically, that's why need invoke delete operator, first invokes destructor calls operator delete release memory.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

javascript - Bootstrap Popover: iOS Safari strange behaviour -

spring cloud - How to configure SpringCloud Eureka instance to point to https on non standard port -