473,770 Members | 7,287 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Destructor Question

Hi,

From 11.11 here, I know that member objects get their dtor's called autmatically:

http://www.parashift.com/c++-faq-lit...html#faq-11.11

What if I have a pointer to a member object?

e.g. (in C++)

struct node
{
~node();

node * next;
}

node::~node()
{
cout << "Should this show once or a ton of times, for a linked list?" << endl;
}

Or inside the dtor, should I be doing this?

node::~node()
{
delete next;
}

Thanks!


Feb 12 '06 #1
7 1869
* dontspam@_dylan _.gov:
Hi,

From 11.11 here, I know that member objects get their dtor's called autmatically:

http://www.parashift.com/c++-faq-lit...html#faq-11.11

What if I have a pointer to a member object?
It doesn't matter if you have a pointer to an object or not.

The destructor is called when the object (not the pointer) is destroyed.

If you have allocated an object via

p = new SomeType;

then the object (not the pointer) is destroyed when you do

delete p;

and since the object is destroyed, its destructor is called.
e.g. (in C++)

struct node
{
~node();

node * next;
}
Missing semicolon.

node::~node()
{
cout << "Should this show once or a ton of times, for a linked list?" << endl;
}

Or inside the dtor, should I be doing this?

node::~node()
{
delete next;
}


Probably neither, but what to do depends -- obviously -- on what you
want to achieve.

If it is your intent that a message should be output on std::cout
whenever a node object is destroyed, do the first. That will make the
node class unusable in a program with a graphical user interface, and in
general, it will make the class unusable for any other purpose than the
original context. So in general, don't do i/o in classes not
specifically designed to do i/o, and in general, don't mix unrelated
things but instead separate them.

If it is your intent that whenever a node object is destroyed it should
also destroy all objects linked via its next pointer, do the second.
That will also constrain the usage of node objects. It seems to belong
at a higher level of abstraction than node does.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 12 '06 #2
I think the point of my question was not seen.

11.11 here states that member objects have their destructors called when
the object itself is deleted (follow this link please!).

http://www.parashift.com/c++-faq-lit...html#faq-11.11

Now, a more detailed code segment (complete with semicolon!).

struct node
{
~node();

node * next;
};

// Say I have a linked list, where the root node is "node * list".

// What does the following do?
delete list;

// Assuming node::~node() is implemented as follows:
node::~node() {} // (Empty)
Does it delete the root node only? Or recursively delete all node's by
calling the destructor's for all the next's? ("next->~node()").

To test this, I make a long linked list, and make the destructor
definition as follows:
node::~node() { cout << "HELLO!" << endl; }

Then I used "delete list;". Sure enough, "HELLO!" was repeated a bunch of
times. This was unexpected to me, I didn't think a destructor
automatically called member object pointer's destructors automatically. I
don't think it mentions anything about it in the C++ FAQ lite.

So, I am running it by the group.
Let me know if that's the expected behavior,
Thanks!


"Alf P. Steinbach" <al***@start.no > wrote in
news:45******** ****@individual .net:
* dontspam@_dylan _.gov:
Hi,

From 11.11 here, I know that member objects get their dtor's called
autmatically:

http://www.parashift.com/c++-faq-lit...html#faq-11.11

What if I have a pointer to a member object?


It doesn't matter if you have a pointer to an object or not.

The destructor is called when the object (not the pointer) is
destroyed.

If you have allocated an object via

p = new SomeType;

then the object (not the pointer) is destroyed when you do

delete p;

and since the object is destroyed, its destructor is called.
e.g. (in C++)

struct node
{
~node();

node * next;
}


Missing semicolon.

node::~node()
{
cout << "Should this show once or a ton of times, for a linked
list?" << endl;
}

Or inside the dtor, should I be doing this?

node::~node()
{
delete next;
}


Probably neither, but what to do depends -- obviously -- on what
you want to achieve.

If it is your intent that a message should be output on std::cout
whenever a node object is destroyed, do the first. That will make the
node class unusable in a program with a graphical user interface, and
in general, it will make the class unusable for any other purpose than
the original context. So in general, don't do i/o in classes not
specifically designed to do i/o, and in general, don't mix unrelated
things but instead separate them.

If it is your intent that whenever a node object is destroyed it
should also destroy all objects linked via its next pointer, do the
second. That will also constrain the usage of node objects. It seems
to belong at a higher level of abstraction than node does.


Feb 12 '06 #3
* NoS_Pam:
[top-posting]
[excessive quoting]
Please don't top-post (corrected). Please don't quote excessively
(corrected). Please read the FAQ (which you have found, good).
* NoS_Pam: * Alf P. Steinbach:
* dontspam@_dylan _.gov:

[question]
[answer]


I think the point of my question was not seen.


It was.
[snip] Now, a more detailed code segment (complete with semicolon!).

struct node
{
~node();

node * next;
};

// Say I have a linked list, where the root node is "node * list".

// What does the following do?
delete list;
It destroys the root node. The root node's destructor is thereby invoked.

// Assuming node::~node() is implemented as follows:
node::~node() {} // (Empty)
In that case only the root node is destroyed and nothing else happens.

Does it delete the root node only?
Yes.

Or recursively delete all node's by
calling the destructor's for all the next's? ("next->~node()").
No.

To test this, I make a long linked list, and make the destructor
definition as follows:
node::~node() { cout << "HELLO!" << endl; }

Then I used "delete list;". Sure enough, "HELLO!" was repeated a bunch of
times.
Human error. Probably you ran an old version of the program.

This was unexpected to me, I didn't think a destructor
automatically called member object pointer's destructors automatically.
It doesn't.

I don't think it mentions anything about it in the C++ FAQ lite.
It does, you have linked to the relevant pages yourself.

So, I am running it by the group.
For the second time now.

Let me know if that's the expected behavior,


With human error involved, anything can happen.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 12 '06 #4

"NoS_Pam" <dontspam@_dyla n_.gov> wrote in message
news:Xn******** **********@130. 215.36.210...
I think the point of my question was not seen.

11.11 here states that member objects have their destructors called when
the object itself is deleted (follow this link please!).

http://www.parashift.com/c++-faq-lit...html#faq-11.11

Now, a more detailed code segment (complete with semicolon!).

struct node
{
~node();

node * next;
};

// Say I have a linked list, where the root node is "node * list".
The declaration:

node *list;

Creates a type 'node *' object. It is not dynamically
allocated (with 'new'), so you must not try to 'delete' it.

// What does the following do?
delete list;
Undefined behavior.

// Assuming node::~node() is implemented as follows:
node::~node() {} // (Empty)
If it doesn't do anything, you need not define it
(the compiler will create it for you).


Does it delete the root node only?
If you 'delete' a pointer whose value was not
the return value from 'new', the behavior is
undefined.
Or recursively delete all node's by
calling the destructor's
When an object is destroyed (whether by going out
of scope or as a result of 'delete'), its destructor
is automatically invoked. A destructor does not
'delete' anything unless it contains code which
explicitly calls 'delete'.

for all the next's? ("next->~node()").
No.

To test this, I make a long linked list, and make the destructor
definition as follows:
node::~node() { cout << "HELLO!" << endl; }

Then I used "delete list;".
Show us the *exact* code you used.
Sure enough, "HELLO!" was repeated a bunch of
times.
It will be repeated for each time the destructor is
called. It doesn't matter whether its called because
the object was 'delete'd or simply went out of scope.
This was unexpected to me, I didn't think a destructor
automatically called member object pointer's destructors automatically.
A destructor will only do what the code inside it tells
it to do.
I
don't think it mentions anything about it in the C++ FAQ lite.


Because that's not how destructors work.

-Mike
Feb 12 '06 #5
In article <Xn************ ******@130.215. 36.210>,
"NoS_Pam" <dontspam@_dyla n_.gov> wrote:
I think the point of my question was not seen.

11.11 here states that member objects have their destructors called when
the object itself is deleted (follow this link please!).

http://www.parashift.com/c++-faq-lit...html#faq-11.11

Now, a more detailed code segment (complete with semicolon!).

struct node
{
~node();

node * next;
};

// Say I have a linked list, where the root node is "node * list".

// What does the following do?
delete list;

// Assuming node::~node() is implemented as follows:
node::~node() {} // (Empty)
Does it delete the root node only? Or recursively delete all node's by
calling the destructor's for all the next's? ("next->~node()").

To test this, I make a long linked list, and make the destructor
definition as follows:
node::~node() { cout << "HELLO!" << endl; }

Then I used "delete list;". Sure enough, "HELLO!" was repeated a bunch of
times. This was unexpected to me, I didn't think a destructor
automatically called member object pointer's destructors automatically. I
don't think it mentions anything about it in the C++ FAQ lite.

So, I am running it by the group.
Let me know if that's the expected behavior,


Given this code:

class A {
public:
~A() { cout << "A::~A()\n" ; }
};

class B {
A* a;
public:
B( A* anA ): a( anA ) { }
~B() { cout << "B::~B()\n" ; }
};

int main() {
A* a( new A );
B* b( new B( a ) );
delete b;
cout << "is A dead?\n";
delete a;
}

The output should be:
B::~B()
is A dead?
A::~A()

Which of course means that B::~B() did not delete it's 'a' object.
Hope this helps.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 12 '06 #6
Hi
Here 'a' is pointer of class A in class B so whenever you are not call
delete on 'a' that 'a' is not deleted.

object is called the destructor not the pointer itself.

Feb 13 '06 #7
Hi
next is pointer if you have not assign a object address that pointer
and delete that pointer that case you got the exception or crash your
application.
first when your constructor should called that assign NULL value to
next.
and in destructor you should code this
if(next) delete next;
otherwise you got the exception.

and one thing you should try so you can better understood.
sturct node{
node next;
};
compile this and check the information given by compiler.

if i am wrong then reply where i am wrong so i can improve my self.

Feb 13 '06 #8

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

Similar topics

52
27040
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object will call it's destructor when it is made a target of a delete".
11
10526
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor" is called in Case 2 since only ~base() becomes "virtual" and ~Derived() is still non-virtual? 3. Does Case 3 show that we don't need any virtual destructor to make ~Derived() called? 4. Is "virtual destructor" needed only for Case 2?
26
3986
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i add a virtual destructor like this : " virtual ~vechile". I get this error: Undefined first referenced symbol in file vtable for vechile /var/tmp//ccC9yD6Z.o
11
5312
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our 4-month old C#/MC++ .NET project project. I'd like to thank in advance anyone who takes the time to read and/or respond to this message. At a couple points, it may seem like a rant against C# / .NET, but we are pretty firmly stuck with this approach...
2
3956
by: arun | last post by:
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.
10
459
by: piboye | last post by:
Hi ! I'm a academician in china. I have been intereted in C++ lasting. In reading the C++ Primer book, i have a trouble about union. In the book ,it said that union can have constructors and destructor ,or other member functions. I can understand using constructors and memeber functions,but what is destructor used for? I have appealled to the forums in chinese ,but no enough usefull feedback.
6
3988
by: Sashi | last post by:
class parent{ Parent(){}; ~Parent(){}; } Child: public Parent{ Child(){}; ~Child(){};
7
2010
by: sam | last post by:
Hi, See when i reading a sourcecode of a program, I read that the constructor is ordinary and after that the programmer has written virtual destructor for that constructor . Why we use the virtual destructor whats the use of it? the code is like this: Network(int input,int output); Network(&Network); virtual ~Network();
3
3859
by: Juha Nieminen | last post by:
I once made my own smart pointer implementation for a project and at one point fought to death to find a malicious bug. The program was not working and I couldn't figure out why. The source of the problem was that MSVC++ was not giving me a warning even though it should have. The problem was that I was creating a smart pointer from a forward-declaration of a class which, it seems, makes it impossible for the smart pointer to call the...
0
9592
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10230
Oralloy
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...
1
10004
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9870
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8886
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, 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...
0
5313
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...
0
5450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
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
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.