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::~Ot herClass() 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::~Ot herClass() get
called?
Thanks
-Laurens 7 2159
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::~Ot herClass() 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::~Ot herClass() get called?
Yes.
Whenever an object is destroyed all subobjects are destroyed too.
Christoph
Thanks for the swift response! This clears things up.
Regards
-Laurens
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::~Ot herClass() 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::~Ot herClass() get called?
There is no difference. Object destruction always works the same,
regardless of how the memory was allocated.
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::~Ot herClass() 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::~Ot herClass() get called?
Thanks -Laurens
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::~Ot herClass() 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::~Ot herClass() get called?
Yes.
Tom
"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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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:
|
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
me some hints on what might be wrong?
// BEGIN OF SAMPLE CODE
class Strategy
|
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: (1)default constructor, (2)copy
constructor, (3)destructor, (4)assignment operator, (5)address-of
operator (non-const), (6)address-of operator (const), in `Effective
|
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 way, trust me". Why?
Is it faster to type? I notice that the derived classes in that example
seem mysteriously not to require a destructor.
|
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 Array. In other words how
do I ensure Destruction of the static members in the Class?
~Foo(){ glbArr.Clear();} this what I have thought of implementing.... Any
suggestions are welcome.
TIA
| |
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 (and rarely). This always happens during
process shutdown, after some thread has called System.Environment.Exit(). Clearly,
some sort of race condition.
Note that what follows only applies to destructors that are called when the process
shuts...
|
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() }
}
|
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 function a1() private. Is there any easy
way to do it?
Thanks,
Peng
|
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
{
blah();
virtual ~blah();
|
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...
|
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...
| |
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |