Wednesday, April 23, 2008

Virtual destructors (What? When? Why?)

There are some questions regarding virtual destructor that I am asked very frequently. Answers to all these questions are similar so I am making a common note on it.

• What is the use of virtual destructor?
• When to make base class destructor virtual?
• Is there any overhead for virtual destructor?
• Why don’t we make all the destructor virtual?
• Will the delete operator be overridden?
• Will the delete[] operator be overridden?



What is the use of virtual destructor?

The virtual destructor comes into play when the class is a base class. If the derived class object is deleted using a base class pointer the necessary destruction of the derived class would not happen if only base class destructor is called. To simplify, if the base class destructor is not virtual and if we try to delete the derived class object using base class pointer the derived class destructor will not be called. It’s known that the destruction is done from derived to base; hence if the derived class destructor is called it will call the base class destructor and the proper destruction of the object will be happening. Altogether by making the base class destructor virtual we are making destructor to be capable of overriding.


When to make base class destructor virtual?

Make the destructor of the class virtual if the class has any virtual function. It should probably save because of 2 reasons.

1. Most of the real world base classes will have at least one virtual function.
2. In the real world problems if there is no virtual function in the base class there is no specific advantage in using base class pointer.

Is there any overhead for virtual destructor?

The per-object-space-cost will be nothing because we only make our destructor private when there is at least one virtual method in the base class. When the first virtual method in the class is made; the per-object-space-cost will be paid off and this makes it costless to make the destructor virtual.

Why don’t we make all the destructor virtual?

If there is no virtual function in the base class and you make the destructor virtual it increases the per-object-space-cost for no added advantage. It is safe not to make the destructor if you class,

1. is not a base class
2. or is not having any virtual function

Will the delete operator be overridden?

Yea, the standards state the delete operator will be overridden and the derived class delete operator will be used for the destruction of derived class.


Will the delete[] operator be overridden?

NO, the standards explicitly states the delete[](deleting array of objects) operator will not be overridden and the operator corresponding to the type of pointer will be used for deleting.

For better understanding on delete and delete[] operator with virtual destruction please read the post Virtual Destructor delete and delete[]

3 comments:

Anonymous said...

What a great web log. I spend hours on the net reading blogs, about tons of various subjects. I have to first of all give praise to whoever created your theme and second of all to you for writing what i can only describe as an fabulous article. I honestly believe there is a skill to writing articles that only very few posses and honestly you got it. The combining of demonstrative and upper-class content is by all odds super rare with the astronomic amount of blogs on the cyberspace.

erectile dysfunction causes said...

There's definately a lot to find out about this issue. I really like all of the points you made.

impotence said...

My family always say that I am killing my time here at web, however I know I am getting knowledge daily by reading such nice content.