473,322 Members | 1,778 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,322 software developers and data experts.

difference between compiler generated destructor and class implemented

Hello Group,

I have a class A, which has three other members and each member is of a
different class
type.

If I don't create a destructor then the compiler will give me one and
whenever class A is
destroyed (whether on stack or on heap) the destructor of each
different object will be called.

However what will happen if the class A defines a destructor like

~A() {} // No code.

The question is that in this case will the destructor of individual
members be called or not,
since the destructor code is emply?

The whole code will look like:

class Object1 {

// Code destructor, constructor

}
class Object2 {

// Code destructor, constructor

}

class Object3 {

// Code destructor, constructor

}

class A {
Object1 o1;
Object2 o2;
Object3 o3;

~A() {} // The question is all about

}
Thanks

nagrik

Jun 12 '06 #1
2 3925

arun wrote:
Hello Group,

I have a class A, which has three other members and each member is of a
different class
type.

If I don't create a destructor then the compiler will give me one and
whenever class A is
destroyed (whether on stack or on heap) the destructor of each
different object will be called.

However what will happen if the class A defines a destructor like

~A() {} // No code.
Same thing really.

Often base classes define this destructor:

virtual ~Base() {}

Same thing as default constructor but is virtual and so this class can
be safely derived.

The question is that in this case will the destructor of individual
members be called or not,
since the destructor code is emply?


Yes. Member variables are destroyed when the lifetime of the
containing object terminates - or is "destroyed".

Jun 12 '06 #2
arun wrote:
Hello Group,

I have a class A, which has three other members and each member is of a
different class
type.

If I don't create a destructor then the compiler will give me one and
whenever class A is
destroyed (whether on stack or on heap) the destructor of each
different object will be called.

However what will happen if the class A defines a destructor like

~A() {} // No code.

The question is that in this case will the destructor of individual
members be called or not,
since the destructor code is emply?

The whole code will look like:

class Object1 {

// Code destructor, constructor

}
class Object2 {

// Code destructor, constructor

}

class Object3 {

// Code destructor, constructor

}

class A {
Object1 o1;
Object2 o2;
Object3 o3;

~A() {} // The question is all about

}
Thanks

nagrik

Just remember that when any object goes out of scope its destructor will
be called. Hence when a object goes out of scope, destructor calls for
all the sub objects that make up the object itself will occur. So unless
the destructor needs to be virtual, allocated memory needs to be taken
care of, or the destruction needs to be flagged elsewhere, it's not
absolutely necessary to code for it. Some people prefer to always code
it for completeness

JB
Jun 12 '06 #3

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

Similar topics

11
by: Shea Martin | last post by:
I have been programming in C++ for over 4 years. I *think* I knew that a struct could have a constructor but I decided to dig into it a little more today, and found that there is very little...
35
by: Maxim Yegorushkin | last post by:
The following code: #include <iostream> class base { private: virtual ~base() { std::cout << "virtual ~base()\n";
0
by: arun | last post by:
Hello Group, I have a class A, which has three other members and each member is of a different class type. If I don't create a destructor then the compiler will give me one and whenever...
2
by: arun | last post by:
Hello Group, The compiler generated destructor will invoke destructor of each member of the containing class let us say 'class A'. However, if I write my own destructor for class A like; ...
22
by: sujilc | last post by:
This question seems to be silly. Can ony one figure out the default functions implemented by compiler when we decalre a class like class A { } According to me this declaration will define...
4
by: moleskyca1 | last post by:
Hi, In a recent discussion, some of us were in disagreement about the functions the C++ compiler generates. How many functions are generated by the compiler when you declare: class Foo { };...
6
by: JohnQ | last post by:
I like, non-copyable, non-assignable and, most often, non-default-constructable also, as a starting point for class design: class SomeClass { SomeClass(); // disallow default construction...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
3
by: C++Liliput | last post by:
Hi, I was looking at the implementation of operator new and operator new in gcc source code and found that the implementation is exactly the same. The only difference is that the size_t argument...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.