473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Member field destructors?

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

Jul 19 '05 #1
7 2150
Laurens wrote:

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

Jul 19 '05 #2
Thanks for the swift response! This clears things up.

Regards
-Laurens
Jul 19 '05 #3
Laurens wrote:
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.

Jul 19 '05 #4
Ian
Laurens wrote:
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

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


Jul 19 '05 #5
On Thu, 14 Aug 2003 10:23:08 +0200, "Laurens" <sp**@block.com> wrote:
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
Jul 19 '05 #6

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bh*************@news.t-online.com...
Laurens wrote:
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.


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
Jul 19 '05 #7
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
22
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give...
3
by: lovecreatesbeauty | last post by:
Predefined class member functions and inheritance How many member functions on earth can be provided (predefined) by standard-compliant compilers? Scott Meyers says that there are 6:...
8
by: Pete C | last post by:
In this section of the FAQ: http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.10 an abstract destructor is given a definition with the cryptic comment "It's faster this...
4
by: Vai2000 | last post by:
Hi All I have a class with static data members in it. How do I un-initialize them ? Looks like if static member is called repeatedly it has residual memory from the last call. the member here is an...
26
by: Michi Henning | last post by:
I've been having problem with destructors in the context of having ported C# code developed under .NET to Mono. What happens is that, on a dual-CPU machine, various parts of the code crash randomly...
14
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
9
by: PengYu.UT | last post by:
Suppose I have class A, which defines a lot of member functions a1 ... an. The class B publically inherent from A, because it want to use A's member function. But B only want make member...
6
by: Jeff Newman | last post by:
Hello, Could anyone explain to me why the following class's destructor shows up as having multiple branches? (At least as judged by gcov 4.1.2 when compiled with gcc 4.1.2 ): struct blah {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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...
0
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...
0
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,...
0
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...

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.