473,624 Members | 2,069 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 3946

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
5705
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 difference between a struct and a class in C++. Both have inheritance, access modifiers, etc. The only diff I see is the default access level is public vs. private. I have always just used structs as a minimal class, as that is the way
35
2159
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 class A is destroyed (whether on stack or on heap) the destructor of each different object will be called.
2
2055
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; class Object1 {
22
2424
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 default functions like
4
9483
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
1963
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 SomeClass(const SomeClass&); // disallow copy construction SomeClass& operator=(const SomeClass&); // disallow assignment public:
43
3801
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 "there is no difference between a struct and a class except the public/private access specification" (and a few minor other things). When I create a class, I always start by declaring the default constructor, copy constructor and assignment operator...
3
3510
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 passed to the operators is calculated correctly during runtime and passed in. Inside the implementation both operators (new and new) do a simple malloc(). Ditto for operator delete/delete. I have two questions: 1) Who passes in the size_t argument...
0
8174
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
8680
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...
0
8624
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7164
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
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
4082
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...
1
2607
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
1786
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1485
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.