what is the use of virtual destructor?
what is the use of virtual destructor?
When object of derived class will be destroyed by invoking the base class destructor then destructor of a class is virtual
http://www.indianculture.co.in/
Virtual destructor is useful at the time when we have a baseclass pointer created with derived class using new. If we try to delete this pointer, it will call the destructor of base class. But, we want the destructor of derived class to be called. So in that case we can make the destructor as virtual. The pseudo code is like this.
Base* a = new Derived();
delete a;
class Base
{
.
.
.
~virtual Base() {
.
}
}
class Derivedublic Base{
.
.
.
~Derived(){
free a;
}
}
A virtual destructor has the keyword virtual in it. If you destroy an object through a caller or reference to a base class, and the base class destructor is not virtual, the derived class destructors are not executed and the destruction is not complete which might lead to memory leaks.
BLAM Ads makes me over $7,000 a month, read my comment here!
I switched my traffic from the others and I'm making more!!!!!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks