Destructor means destroy the class.The virtual attributes declared the virtual destructor. A base class reference destroy by an object through a pointer but base class destructor is not virtual.the derived-class destructors are not executed, and the destruction might not be complete.
Ex:
struct X{ virtual ~Y() {} virtual X * Clone() { return new X; } }; struct Y : public X { virtual X * Clone() { return new Y; } };
int main() { X * x1 = new Y; X * x2 = x1->Clone(); // virtual construction delete x2; delete x1; }
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Virtual Destructor :
Destructor means destroy the class.The virtual attributes declared the virtual destructor. A base class reference destroy by an object through a pointer but base class destructor is not virtual.the derived-class destructors are not executed, and the destruction might not be complete.
Ex:
struct X{virtual ~Y() {}
virtual X * Clone() { return new X; }
};
struct Y : public X {
virtual X * Clone() { return new Y; }
}; int main() {
X * x1 = new Y;
X * x2 = x1->Clone(); // virtual construction
delete x2;
delete x1;
}