473,407 Members | 2,359 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.

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 1820
* 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@_dylan_.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@_dylan_.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
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...
11
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"...
26
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...
11
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...
2
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...
10
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...
6
by: Sashi | last post by:
class parent{ Parent(){}; ~Parent(){}; } Child: public Parent{ Child(){}; ~Child(){};
7
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...
3
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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:
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...
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.