Destructors, pointers and scope

L

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 implemented? Or do I need to delete
any downstream pointers manually?

TIA
 
A

Alf P. Steinbach

* 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/>.
 
L

Lilith

* 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
 
E

Eyeless

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...
 
F

Ferdi Smit

Eyeless said:
Hi!


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: (e-mail address removed)
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top