a quick question. Is there a reason not to declare any destructor as
virtual (aside from possible potential performance issues) ?
The MSVC 2005 class wizard asks me upon class creation if I want virtual
or not, while my older compiler always defined it as virutal.
Cheers,
Giulio.
If you are declaring a class which is not designed to be extended (i.e.
nothing should inherit from it) nor used polymorphically, there is no
need for a virtual destructor.
If your class is designed to be used polymorphically, i.e. as the base
class for dependent classes, then it *must* be declared virtual if your
code calls delete on a pointer to the base class which actually points
to an object of some derived type. Otherwise, there is undefined behavior.
If your class is derived from some other base class, and that base class
has a virtual destructor, then the destructor of the derived class (and
all other functions which override functions in the base class) is
automatically virtual ... i.e., there is no need to write it explicitly
with the "virtual" keyword.
HTH
You need a virtual destructor if you are going to delete objects of
derived classes through a pointer to the base class. If you don't do
that, there is not much use for making the destructor virtual.
If you have no other virtual functions in the class, the virtual
destructor forces the creation of a vtable and adds a pointer to the
class size. If you have lots of classes or lots of objects, this might
make a difference. Or not!
Bo Persson
There are operations/functions which are only valid for PODs (memcpy
comes to mind). By adding a virtual destructor, when perhaps you
wouldn't have any destructor at all, (and would otherwise be POD),
excludes the use of these functions. It also reduces serialization
possibilities, and needlessly increases the size of your objects.
Once you make all destructors virtual, why not make all functions
virtual "just in case" ?
Joe Cook
Yes -- documentation. If you make it virtual, readers will think it's
for a reason.
> The MSVC 2005 class wizard asks me upon class creation if I want virtual
> or not, while my older compiler always defined it as virutal.
There are *wizards* for a trivial task like this?
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .