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
Post a Comment