Destructors, pointers and scope

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lilith

    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
  • Alf P. Steinbach

    #2
    Re: Destructors, pointers and scope

    * Lilith:[color=blue]
    > 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?[/color]

    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_p tr -- 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?

    Comment

    • Lilith

      #3
      Re: Destructors, pointers and scope

      On Sun, 19 Feb 2006 20:42:07 +0100, "Alf P. Steinbach"
      <alfps@start.no > wrote:
      [color=blue]
      >* Lilith:[color=green]
      >> 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?[/color][/color]
      [color=blue]
      >When you delete a pointer, the pointed to object is destroyed, and when
      >an object of class type is destroyed, its destructor is executed.[/color]
      [color=blue]
      >However, instead of doing list management using raw pointers, consider
      >using std::list.[/color]

      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.
      [color=blue]
      >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_p tr -- the
      >latter is part of the Boost library, at <url: http://www.boost.org/>.[/color]

      I'll be taking a look.

      Thanks,
      Lilith

      Comment

      • Eyeless

        #4
        Re: Destructors, pointers and scope

        Hi![color=blue]
        >std::auto_pt r or (probably best for your
        >needs, because std::auto_ptr is tricky) boost::shared_p tr[/color]
        It will really be better to use boost::shared_p tr 'cause std::auto_ptr
        has very special copy semantics and may confuse you if you'd like to
        copy one list to another...

        Comment

        • Ferdi Smit

          #5
          Re: Destructors, pointers and scope

          Eyeless wrote:[color=blue]
          > Hi!
          >[color=green]
          >>std::auto_p tr or (probably best for your
          >>needs, because std::auto_ptr is tricky) boost::shared_p tr[/color]
          >
          > It will really be better to use boost::shared_p tr 'cause std::auto_ptr
          > has very special copy semantics and may confuse you if you'd like to
          > copy one list to another...
          >[/color]

          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: Ferdi.Smit@cwi. nl
          Room: C0.07 Phone: 4229
          INS3 Visualization and 3D Interfaces
          CWI Amsterdam, The Netherlands

          Comment

          Working...