Sunday, 15 March 2015

c++ - Default destructor when using pointer to base class -



c++ - Default destructor when using pointer to base class -

class { int a; }; class b : public { int b; }; int main(void) { * p = new b; delete p; // (1) homecoming 0; }

in above code both classes have default compiler-generated destructors. both classes have plain old info members don't need manually written d'tors free resources. question - after phone call in (1) default destructor free entirety of b's instance or there memory leaks? know utilize virtual destructor here i'm not sure how default d'tors behave.

what trying invokes undefined behavior, declaring destructor virtual can considered mandatory.

delete p

will seek delete p instance of a since destructor not declared virtual right runtime implementation not called.

mind doesn't happen when don't have pointer concrete object, eg

a = b();

because object slicing occurs before, when a exits scope it's a

c++ polymorphism default destructor

No comments:

Post a Comment