By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

difference between compiler generated destructor and class implemented

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.