Virtual Destructor
1678
20-Jun-2018
What is virtual Destructor?
Prakash nidhi Verma
20-Jun-2018Virtual 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;
}