Works in the same way as virtual functions, used only when you want to specify which destructor you want to call.
---- Scenario 1 ----
class Parent{
    ~Parent();
};
class Child : public Parent{
    ~Child();
};
Parent* kid1 = new Child;
Child* kid2 = new Child;
delete kid1;        // calls parent destructor because Parent.~Parent is not virtual
delete kid2;        // calls parent destructor followed by child destructor (usual behavior)
---- Scenario 2 ----
class Parent{
    virtual ~Parent();
};
class Child : public Parent{
    ~Child();
};
Parent* kid1 = new Child;
Child* kid2 = new Child;
delete kid1;        // calls child destructor because Parent.~Parent is virtual
delete kid2;        // calls parent destructor followed by child destructor (usual behavior)
No comments:
Post a Comment