473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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?

Jun 14 '07 #1
12 2845
On Jun 13, 10:35 pm, Avalon1178 <Avalon1...@gmail.comwrote:
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.

Jun 14 '07 #2

"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
Jun 14 '07 #3
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.

Jun 14 '07 #4
On Jun 14, 1:35 pm, Avalon1178 <Avalon1...@gmail.comwrote:
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.

Jun 14 '07 #5
On Jun 14, 9:35 am, Avalon1178 <Avalon1...@gmail.comwrote:
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

Jun 14 '07 #6
On Jun 14, 8:38 am, "Robbie Hatley"
<see.my.signat...@for.my.email.addresswrote:
"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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 14 '07 #7
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.
Jun 14 '07 #8
"Robbie Hatley" <se**************@for.my.email.addresswrote in message
news:ln*******************@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.
Jun 14 '07 #9
<in*************@gmail.comwrote in message
news:11*********************@j4g2000prf.googlegrou ps.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.
Jun 14 '07 #10
"Ge Chunyuan" <hh****@gmail.comwrote in message
news:11**********************@q19g2000prn.googlegr oups.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.
Jun 14 '07 #11
On Jun 14, 8:30 pm, terminator <farid.mehr...@gmail.comwrote:
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.

Jun 14 '07 #12
On Jun 15, 1:26 am, Old Wolf <oldw...@inspire.net.nzwrote:
On Jun 14, 8:30 pm, terminator <farid.mehr...@gmail.comwrote:
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.
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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 15 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: ctick | last post by:
When defining a clas and no constructor and destructor provided, compiler generates both. What're the need for this since they do nothing as to constructing/destructing an obejct. What's...
1
by: Zahid Faizal | last post by:
I am extremely surprised that my C++ compiler does not invoke the destructor on dynamically created objects upon program termination. As part of the overall "cleanup" upon program termination I...
46
by: sbayeta | last post by:
Hi, I'd like to know who is responsible of memory recycling and defragmentation in a C/C++ program, assuming all the memory allocation/deallocation is done using malloc/free or new/delete. ...
8
by: Edward Diener | last post by:
I have a __value class which uses some legacy C++ code. So I wrapped the legacy C++ code in another __nogc class and have a pointer to that class as a member of my __value class. When the __value...
9
by: Hasani \(remove nospam from address\) | last post by:
I was reading a ppt ( http://www.gotdotnet.com/team/pdc/4064/tls310.ppt ) and came aross this statement. "Users can leverage a destructor. The C++ compiler generates all the Dispose code...
7
by: | last post by:
Hi, From 11.11 here, I know that member objects get their dtor's called autmatically: http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.11 What if I have a pointer to a member object?...
3
by: Pravesh | last post by:
Hello All, I had some query regarding virtual functions/destructors. If a class is having some/all of its methods that are virtual then is it recommended that it should also have virtual...
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
9
by: gabest | last post by:
I've got the following situation, simplified example of course. The thread would be deep behind interfaces, classes, etc. Is there any way to automagically exit it without having to add an Exit()...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.