Lets talk about Virtual Destructors...
-
A vitrual destructor is just the destructor of the class made virtual. (i.e. virtual ~MyClass ()). What it allows you to do is have methods in a base class handle the deletion of the object and still have the proper set of destructors be invoked. A perfect example from COM is having the base class define the Release method where it deletes the object when the ref count goes to zero. Tim Smith Descartes Systems Sciences, Inc.
-
A vitrual destructor is just the destructor of the class made virtual. (i.e. virtual ~MyClass ()). What it allows you to do is have methods in a base class handle the deletion of the object and still have the proper set of destructors be invoked. A perfect example from COM is having the base class define the Release method where it deletes the object when the ref count goes to zero. Tim Smith Descartes Systems Sciences, Inc.
and... The disadvantage of having a virtual destructor is that if you have one, but have no virtual functions, then you are still being hit with a virtual table pointer. So you really shouldn't include a virtual destructor unless you are going to derive from the class. (and usually a good indication of that is if you have any virtual functions!) Paul. Have fun, Paul Westcott.
-
This seems to be a popular interview question. I've been asked this in about 1/3 of my job interviews over the past ~7 months. --Mike-- http://home.inreach.com/mdunn/ All your base are belong to ME~!
-
This seems to be a popular interview question. I've been asked this in about 1/3 of my job interviews over the past ~7 months. --Mike-- http://home.inreach.com/mdunn/ All your base are belong to ME~!
-
and... The disadvantage of having a virtual destructor is that if you have one, but have no virtual functions, then you are still being hit with a virtual table pointer. So you really shouldn't include a virtual destructor unless you are going to derive from the class. (and usually a good indication of that is if you have any virtual functions!) Paul. Have fun, Paul Westcott.
-
This seems to be a popular interview question. I've been asked this in about 1/3 of my job interviews over the past ~7 months. --Mike-- http://home.inreach.com/mdunn/ All your base are belong to ME~!
-
What do you mean by "being hit with a virtual table pointer" ? How come this is an advantage? Ammar
not advantage - i said disadvantage (you have an extra 4 bytes per object) Paul. Have fun, Paul Westcott.