473,499 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3932

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
5689
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
2132
by: Maxim Yegorushkin | last post by:
The following code: #include <iostream> class base { private: virtual ~base() { std::cout << "virtual ~base()\n";
0
285
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
2050
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
2396
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
9461
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
1953
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
3769
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
3503
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
7134
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
7012
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
7180
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
7392
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...
1
4920
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...
0
4605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1429
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 ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
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...

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.