difference between compiler generated destructor and class implemented

A

arun

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
 
N

Noah Roberts

arun said:
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".
 
N

n2xssvv g02gfr12930

arun said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top