Destructors, pointers and scope

Discussion in 'C++' started by Lilith, Feb 19, 2006.

  1. Lilith

    Lilith Guest

    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
    Lilith, Feb 19, 2006
    #1
    1. Advertising

  2. * 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?
    Alf P. Steinbach, Feb 19, 2006
    #2
    1. Advertising

  3. Lilith

    Lilith Guest

    On Sun, 19 Feb 2006 20:42:07 +0100, "Alf P. Steinbach"
    <> 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
    Lilith, Feb 19, 2006
    #3
  4. Lilith

    Eyeless Guest

    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...
    Eyeless, Feb 19, 2006
    #4
  5. Lilith

    Ferdi Smit Guest

    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:
    Room: C0.07 Phone: 4229
    INS3 Visualization and 3D Interfaces
    CWI Amsterdam, The Netherlands
    Ferdi Smit, Feb 20, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    658
    Ron Natalie
    Dec 10, 2004
  2. Takeshi
    Replies:
    3
    Views:
    540
    Takeshi
    Nov 1, 2004
  3. Steven T. Hatton
    Replies:
    9
    Views:
    458
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    653
  5. Andrew Falanga
    Replies:
    2
    Views:
    191
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page