question about default destructors...



 DEVELOP > c-Plus-Plus > question about default destructors...

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Avalon1178"
Date: 14 Jun 2007 12:35:02 AM
Object: question about default destructors...
Hello,
Are default destructors virtual?
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?
.

User: "Robbie Hatley"

Title: Re: question about default destructors... 14 Jun 2007 01:38:11 AM
"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.

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. If B has dynamically-allocated
data members which A doesn't, that will create a memory leak.
In a case like that, you should put this line in A:
class A
{
public:
A();
virtual ~A();
...
private:
...
};
Then make B's destructor do whatever it needs to do. (Such
as calling "delete" for new'd data members.)
class B : public A
{
public:
B() {Blat = new double;}
~B() {delete Blat;}
...
private:
double *Blat;
...
};
Then if you do this in main():
int main (void)
{
A* Fred = new B;
delete Fred; // calls ~B(), not ~A()
return 42;
}
Fred will then get correctly destructed.
--
Cheers,
Robbie Hatley
lone wolf aatt well dott com
triple-dubya dott Tustin Free Zone dott org
.
User: "Andrew Koenig"

Title: Re: question about default destructors... 14 Jun 2007 11:47:40 AM
"Robbie Hatley" <see.my.signature@for.my.email.address> wrote in message
news:ln5ci.84543$ds2.82129@fe02.news.easynews.com...

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.

Not true. You need a virtual destructor in your base class whenever you use
a pointer to base to delete a derived-class object. Just because your
implementation happens not to enforce the rule doesn't make the rule go
away.
.

User: ""

Title: Re: question about default destructors... 14 Jun 2007 02:37:11 AM

int main (void)
{
A* Fred = new B;
delete Fred; // calls ~B(), not ~A()

IIRC, ~A() will still be called after the B object is destructed.
.
User: "Andrew Koenig"

Title: Re: question about default destructors... 14 Jun 2007 11:48:23 AM
<indrawati.yahya@gmail.com> wrote in message
news:1181806631.134539.82060@j4g2000prf.googlegroups.com...

int main (void)
{
A* Fred = new B;
delete Fred; // calls ~B(), not ~A()


IIRC, ~A() will still be called after the B object is destructed.

If class A does not have a virtual destructor, the effect of this example is
undefined, which means that anything could happen.
.


User: "James Kanze"

Title: Re: question about default destructors... 14 Jun 2007 04:20:04 AM
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 =3D new A;
B * b =3D new B;
a =3D 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
.


User: "Ron Natalie"

Title: Re: question about default destructors... 14 Jun 2007 06:33:34 AM
Avalon1178 wrote:

Hello,

Are default destructors virtual?

No.


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?

It's worse than the destructors not being called right. It's
outright undefined behavior. The language specifically requires
a virtual destructor to delete derived objects through that type.
The implementation is free to key all sorts of stuff (like the
deallocation function) off the presence of the virtual destructor.
.

User: ""

Title: Re: question about default destructors... 14 Jun 2007 12:50:19 AM
On Jun 13, 10:35 pm, Avalon1178 <Avalon1...@gmail.com> wrote:

Hello,

Are default destructors virtual?

No, they are not. The rationale is that you should not have to pay for
features you don't use, and sone classes do not need virtual
destructors.
.

User: "Ge Chunyuan"

Title: Re: question about default destructors... 14 Jun 2007 03:18:48 AM
On Jun 14, 1:35 pm, Avalon1178 <Avalon1...@gmail.com> wrote:

Hello,

Are default destructors virtual?

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?

No, compiler won't generate default virtual desctrutor.
Once you delete a, it will just invoke A's destructor of course.
You can debug this sample code in the IDE and check whether there is
vptr.
.
User: "Andrew Koenig"

Title: Re: question about default destructors... 14 Jun 2007 11:49:02 AM
"Ge Chunyuan" <hhygcy@gmail.com> wrote in message
news:1181809128.829848.300790@q19g2000prn.googlegroups.com...


A * a = new A;
B * b = new B;
a = b;
delete a;

No, compiler won't generate default virtual desctrutor.
Once you delete a, it will just invoke A's destructor of course.

Maybe yes, maybe no.
.


User: "terminator"

Title: Re: question about default destructors... 14 Jun 2007 03:30:13 AM
On Jun 14, 9:35 am, Avalon1178 <Avalon1...@gmail.com> wrote:

Hello,

Are default destructors virtual?

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?

it calls A .but from the point at which you define a virtual
destructor ,subsequent derived classes will have virtual versions of
dtor eventhough you do not declare any:
struct A{};
struct B:A{virtual ~B(){};};
struct C:B{};
struct D:C{~D{};};
A* a = new B;
delete a;//call A`s dtor .dtor is not virtual yet
B* b = new c;
delete b;//call C`s dtor. after B dtor will be virtual
delete (b = new D);//call D`s dtor
C* c = new D;
delete c;//call D`s dtor
regards,
FM
.
User: "Old Wolf"

Title: Re: question about default destructors... 14 Jun 2007 06:26:34 PM
On Jun 14, 8:30 pm, terminator <farid.mehr...@gmail.com> wrote:

struct A{};
struct B:A{virtual ~B(){};};
struct C:B{};
struct D:C{~D{};};

A* a = new B;
delete a;//call A`s dtor .dtor is not virtual yet

No, this causes undefined behaviour. (As has been
pointed out several times on this thread already
but I thought I'd mention it again in case anyone
was under the impression that this example is
different to the other examples presented).

B* b = new c;

I guess you mean C rather than c.

delete b;//call C`s dtor.

Calls C's dtor and then B's dtor, to be precise.

delete (b = new D);//call D`s dtor

Is that legal? i.e. delete (X=Y)
First impression is that it might fall foul of
sequence point rules and/or the rule about
accessing the result of an assignment.
.
User: "James Kanze"

Title: Re: question about default destructors... 15 Jun 2007 04:51:09 AM
On Jun 15, 1:26 am, Old Wolf <oldw...@inspire.net.nz> wrote:

On Jun 14, 8:30 pm, terminator <farid.mehr...@gmail.com> wrote:

delete (b =3D new D);//call D`s dtor

Is that legal? i.e. delete (X=3DY)
First impression is that it might fall foul of
sequence point rules and/or the rule about
accessing the result of an assignment.

Good question. I think it is, because delete resolves to a
function call (or two, if there is a destructor), and a function
call implies a sequence point; if b is a global variable, and
the destructor of D reads it, it must see the effect of the
assignment, and if the global operator delete has been replaced,
it must also see the effect of the assignment.
On the other hand, it doesn't look too useful, and I don't think
I'd recommend it in production code.
--
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
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER