inheritance - Detructors and inherited functions c++ -


if have 2 classes this:

class {    public:    virtual print(){};    ~a(){print();} }; class b:public {    public:    print(){};    ~b(){} }; void main() {   b *b1=new b;   delete b1; } 

in destructor in class call print class , not b because when in class destructor,class b technically destructed?

yes, thats right. class destructed calling destructor itself, , destructor parent classes, means time destruct a, b gone. see similar behaviour if invoke virtual, overriden functions in base classes when constructing.

it considered bad practice call virtual functions in constructors or destructors, behaviour, while well-defined, can misleading uninitiated. it's easy trip if initiated.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -