473,406 Members | 2,345 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,406 software developers and data experts.

Destructors, pointers and scope

I'm working on a class that contains a pointer to a different class
object that behaves as a linked list. What I'm not sure of is, if I
do a delete on the pointer to the object of the second class, will the
destructor of the second class be implemented? Or do I need to delete
any downstream pointers manually?

TIA
--
Lilith
Feb 19 '06 #1
4 1696
* Lilith:
I'm working on a class that contains a pointer to a different class
object that behaves as a linked list. What I'm not sure of is, if I
do a delete on the pointer to the object of the second class, will the
destructor of the second class be [executed]? Or do I need to delete
any downstream pointers manually?


When you delete a pointer, the pointed to object is destroyed, and when
an object of class type is destroyed, its destructor is executed.

However, instead of doing list management using raw pointers, consider
using std::list.

And if you must use pointers, for some reason, consider packing them in
smart pointer objects such as std::auto_ptr or (probably best for your
needs, because std::auto_ptr is tricky) boost::shared_ptr -- the
latter is part of the Boost library, at <url: http://www.boost.org/>.

--
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 19 '06 #2
On Sun, 19 Feb 2006 20:42:07 +0100, "Alf P. Steinbach"
<al***@start.no> wrote:
* Lilith:
I'm working on a class that contains a pointer to a different class
object that behaves as a linked list. What I'm not sure of is, if I
do a delete on the pointer to the object of the second class, will the
destructor of the second class be [executed]? Or do I need to delete
any downstream pointers manually?
When you delete a pointer, the pointed to object is destroyed, and when
an object of class type is destroyed, its destructor is executed. However, instead of doing list management using raw pointers, consider
using std::list.
You're probably right. I just got used to using linked structures in
years past. I was out of programming for a while was never really in
mainstream programming to begin with. So I didn't keep up with
developing tools.
And if you must use pointers, for some reason, consider packing them in
smart pointer objects such as std::auto_ptr or (probably best for your
needs, because std::auto_ptr is tricky) boost::shared_ptr -- the
latter is part of the Boost library, at <url: http://www.boost.org/>.


I'll be taking a look.

Thanks,
Lilith
Feb 19 '06 #3
Hi!
std::auto_ptr or (probably best for your
needs, because std::auto_ptr is tricky) boost::shared_ptr

It will really be better to use boost::shared_ptr 'cause std::auto_ptr
has very special copy semantics and may confuse you if you'd like to
copy one list to another...

Feb 19 '06 #4
Eyeless wrote:
Hi!
std::auto_ptr or (probably best for your
needs, because std::auto_ptr is tricky) boost::shared_ptr


It will really be better to use boost::shared_ptr 'cause std::auto_ptr
has very special copy semantics and may confuse you if you'd like to
copy one list to another...


Also don't forget that an STL container cannot contain std::auto_ptr,
the C++ standard explicitly disallows this! So even if you could work
around all the tricky issues, it's not even allowed.

--
Regards,

Ferdi Smit (M.Sc.)
Email: Fe********@cwi.nl
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
Feb 20 '06 #5

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

Similar topics

3
by: joe martin | last post by:
Does anyone know when or if destructors for statically declared objects are called? I am just wondering if I am leaking system resources each time i exit my program. Thanks, -Joe ...
7
by: Laurens | last post by:
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...
9
by: Magalie | last post by:
Hi! I've got a question concerning C++ exceptions, more precisely the stack unwinding when an exception occurs. We agree that when an exception is thrown, all objects that have destructors are...
7
by: joevandyk | last post by:
Below, I have a class Container that contains a vector. The vector contains pointers to a Base class. When the Container destructor is called, I would like to delete all the objects that the...
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...
8
by: Edward Diener | last post by:
I have a __value class which uses some legacy C++ code. So I wrapped the legacy C++ code in another __nogc class and have a pointer to that class as a member of my __value class. When the __value...
9
by: Peter Oliphant | last post by:
I would assume in the following that 'instance' being set to 'nullptr' should cause the instance of myClass that was created to no longer have any pointers pointing to it, and therefore be...
6
by: mlw | last post by:
Could someone explain why there is no destructor in Java classes? There are many times you need to be called WHEN an object goes out of scope and not when it will eventally be freed.
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
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,...
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,...

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.