473,654 Members | 3,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2866
On Jun 13, 10:35 pm, Avalon1178 <Avalon1...@gma il.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...@gma il.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...@gma il.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 objektorientier ter Datenverarbeitu ng
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******** ***********@fe0 2.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.comwrot e in message
news:11******** *************@j 4g2000prf.googl egroups.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

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

Similar topics

8
3260
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 happening in constructor/destructor if they both are defaulted and empty? Thanks!
1
1397
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 was expecting the destructors of all the dynamically created objects to get invoked. Is this a quirk of my compiler, or is this the norm? I know that I am a "nobody", but this does not make any sense to me at all. Thanks, Zahid
46
4132
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. Thanks
8
1803
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 class is created, I dynamically allocate an object of the class with the legacy C++ code. However because the __value class has no destructor, I can never release that allocated memory. Why does a __value class allow no destructor ? Without it I...
9
1249
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 automatically, including chaining calls to Dispose. (There is no Dispose pattern)" but Dispose can thrown an exception. Is the exception supressed?
7
1864
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? e.g. (in C++)
3
2061
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 destructor? When I am defining such a class with default destructor then my compiler is giving warning that class XXX has virtual functions but non-virtual destructor.
7
2110
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 BufferBase, and customized in order to be able to a type of data (of any kind, such as chunks of audio samples from a sound card). The processor classes are derived from ProcessorBase, and are customized to handle BufferBase-derived objects. Each...
9
1497
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() call to everywhere? I just want to break that loop when the application is done (the thread is still running so it cannot be done, but I think there is no more reference from the root object, so it could call the destructor at least), or when the...
0
8375
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
6161
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1593
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.