Re: question about default destructors...
On Jun 14, 8:38 am, "Robbie Hatley"
<see.my.signat...@for.my.email.address> wrote:
"Avalon1178" wrote:
Are default destructors virtual?
Nope. Just last night, my compiler (gcc) warned me:
"Warning: class has virtual functions but non-virtual
destructor".
You really only need a virtual destructor if your base class
has virtual functions, and if your derived classes have data
members that need to be destructed differently than than the
base class.
I don't know where you got that from, but it is completely
false. If you delete an object of a derived type through a
pointer to the base type, the destructor must be virtual in the
base type. It certainly has nothing to do with what is or is
not present in the derived class---if the dynamic type is
different from the static type, either the destructor in the
static type is virtual, or you have undefined behavior.
Typically, of course, if a class doesn't have virtual functions,
there's no point in deriving from it, or at least, there's no
point in having a pointer to it when the actual object has a
derived type. So typically, if you don't have virtual
functions, you don't need a virtual destructor. Typically;
there are a few rare cases where you might use some sort of
labeling interface, which has no functions of its own.
In other words, say I have "class A" and "class B : public A",
and I have the code below:
A * a = new A;
B * b = new B;
a = b;
delete a;
Neither A or B have destructors defined (thus creating a default
destructor for each), when "delete a" is called, will it call b's
destructor first then a's destructor, or will it just call a's
destructor?
That will call A's destructor.
Maybe. I might also crash the system. Or reformat your hard
drive. It's undefined behavior.
--
James Kanze (GABI Software, from CAI) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34