Member field destructors?

L

Laurens

Hi,


My C++ is pretty rusty after years of using Java, I can't recall the exact
details of how destructors work.

I know that a class's destructor is automagically called when an instance
goes out of scope(when it unwinds from the stack). However, I can't recall
whether destructors of member fields get called automatically when this
happens.

Example class:

class MyClass {
private:
OtherClass otherClass();
public:
MyClass();
~MyClass();
};


Example usage:

MyClass myClass(); // Object allocated on stack
<myClass goes out of scope>
// What happens here? Does OtherClass::~OtherClass() get called?


Another example usage, this time with new and delete:

MyClass *myClass = new MyClass(); // Allocated on heap
delete myClass; // What happens here? Does OtherClass::~OtherClass() get
called?


Thanks
-Laurens
 
C

Christoph Rabel

Laurens said:
Example class:

class MyClass {
private:
OtherClass otherClass();
public:
MyClass();
~MyClass();
};


Example usage:

MyClass myClass(); // Object allocated on stack
<myClass goes out of scope>
// What happens here? Does OtherClass::~OtherClass() get called?
Yes.

Another example usage, this time with new and delete:

MyClass *myClass = new MyClass(); // Allocated on heap
delete myClass; // What happens here? Does OtherClass::~OtherClass() get
called?

Yes.
Whenever an object is destroyed all subobjects are destroyed too.

Christoph
 
R

Rolf Magnus

Laurens said:
Hi,


My C++ is pretty rusty after years of using Java, I can't recall the
exact details of how destructors work.

I know that a class's destructor is automagically called when an
instance goes out of scope(when it unwinds from the stack). However, I
can't recall whether destructors of member fields get called
automatically when this happens.

Example class:

class MyClass {
private:
OtherClass otherClass();
public:
MyClass();
~MyClass();

This class has no member variables. If otherClass is supposed to be one,
then this is not correct. It is instead a member function taking no
arguments and returning an OtherClass object.
Example usage:

MyClass myClass(); // Object allocated on stack
<myClass goes out of scope>
// What happens here? Does OtherClass::~OtherClass() get called?

No, since the object has no member of that class.
Another example usage, this time with new and delete:

MyClass *myClass = new MyClass(); // Allocated on heap
delete myClass; // What happens here? Does OtherClass::~OtherClass()
get called?

There is no difference. Object destruction always works the same,
regardless of how the memory was allocated.
 
I

Ian

Laurens said:
Hi,


My C++ is pretty rusty after years of using Java, I can't recall the exact
details of how destructors work.

I know that a class's destructor is automagically called when an instance
goes out of scope(when it unwinds from the stack). However, I can't recall
whether destructors of member fields get called automatically when this
happens.

Example class:

class MyClass {
private:
OtherClass otherClass();
public:
MyClass();
~MyClass();
};


Example usage:

MyClass myClass(); // Object allocated on stack
<myClass goes out of scope>
// What happens here? Does OtherClass::~OtherClass() get called?

Yes, otherClass goes out of scope when its containing class is destroyed.

Ian
 
L

Laurens

Rolf Magnus said:
This class has no member variables. If otherClass is supposed to be one,
then this is not correct. It is instead a member function taking no
arguments and returning an OtherClass object.

You are right. The intention was to illustrate my question with an example,
I didn't actually compile the code.

It should be:
OtherClass otherClass;


Regards
-Laurens
 
T

tom_usenet

Hi,


My C++ is pretty rusty after years of using Java, I can't recall the exact
details of how destructors work.

I know that a class's destructor is automagically called when an instance
goes out of scope(when it unwinds from the stack). However, I can't recall
whether destructors of member fields get called automatically when this
happens.

Example class:

class MyClass {
private:
OtherClass otherClass();

That declares a private member function. I assume you meant:

OtherClass otherClass;
public:
MyClass();
~MyClass();
};


Example usage:

MyClass myClass(); // Object allocated on stack

That declares a non-member function (called myClass, that takes no
parameters and returns a MyClass object). I think you meant to declare
an object:

MyClass myClass;
<myClass goes out of scope>
// What happens here? Does OtherClass::~OtherClass() get called?

Yes, assuming the changes above.
Another example usage, this time with new and delete:

MyClass *myClass = new MyClass(); // Allocated on heap
delete myClass; // What happens here? Does OtherClass::~OtherClass() get
called?

Yes.

Tom
 
K

Kevin Goodsell

tom_usenet wrote:
[The only completely correct answer (at least of the ones I've seen) so
far.]

Tom got it right, in case there's any confusion as to who was correct.

-Kevin
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top