auto_ptr: reassigning to member of owned object

Discussion in 'C++' started by Joost Ronkes Agerbeek, Oct 2, 2003.

  1. Is it legal to reassign an auto_ptr to another auto_ptr that is a member of
    the object the auto_ptr is holding before the reassignment? (Confused yet?
    :p)

    #include <memory>
    class Node
    {
    public:
    Node() : next(0) {}
    Node(Node* node) : next(node) {}
    std::auto_ptr<Node> next;
    };

    void main()
    {
    Node node(new Node(new Node()));
    node.next = (*node.next).next; // <-- is this valid?
    }

    As I see it, this program could cause problems if the object node.next owns
    is deleted before (*node.next).next is evaluated. I even think the auto_ptr
    assignment operator needs to make a temporary copy of (*node.next).next.
    The above code compiles and runs on my system, but is it legal according to
    the C++ Standard?

    tia,
    Joost
    Joost Ronkes Agerbeek, Oct 2, 2003
    #1
    1. Advertising

  2. Joost Ronkes Agerbeek wrote:
    > Is it legal to reassign an auto_ptr to another auto_ptr that is a member of
    > the object the auto_ptr is holding before the reassignment? (Confused yet?
    > :p)
    >
    > #include <memory>
    > class Node
    > {
    > public:
    > Node() : next(0) {}
    > Node(Node* node) : next(node) {}
    > std::auto_ptr<Node> next;
    > };
    >
    > void main()


    obligatory - main must return int comment.

    > {
    > Node node(new Node(new Node()));
    > node.next = (*node.next).next; // <-- is this valid?
    > }
    >
    > As I see it, this program could cause problems if the object node.next owns
    > is deleted before (*node.next).next is evaluated.


    That's kind of hard since operator= can't be called until
    (*node.next).next is evaluated.

    I even think the auto_ptr
    > assignment operator needs to make a temporary copy of (*node.next).next.
    > The above code compiles and runs on my system, but is it legal according to
    > the C++ Standard?
    Gianni Mariani, Oct 3, 2003
    #2
    1. Advertising

  3. > > void main()
    > obligatory - main must return int comment.

    You're right of course. My mistake.

    > > {
    > > Node node(new Node(new Node()));
    > > node.next = (*node.next).next; // <-- is this valid?
    > > }
    > >
    > > As I see it, this program could cause problems if the object node.next

    owns
    > > is deleted before (*node.next).next is evaluated.

    >
    > That's kind of hard since operator= can't be called until
    > (*node.next).next is evaluated.


    That makes sense. :-D

    Problem solved then :), thanks a bunch.
    Joost Ronkes Agerbeek, Oct 3, 2003
    #3
    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. Berco
    Replies:
    6
    Views:
    923
    Roedy Green
    Jan 8, 2004
  2. tolu45
    Replies:
    4
    Views:
    4,506
    Roedy Green
    Mar 29, 2006
  3. Siemel Naran

    auto_ptr<Derived> to auto_ptr<Base>

    Siemel Naran, Jan 10, 2005, in forum: C++
    Replies:
    2
    Views:
    1,534
    Dave Rahardja
    Jan 11, 2005
  4. tech
    Replies:
    7
    Views:
    366
    Diego Martins
    Jun 23, 2008
  5. Sousuke
    Replies:
    9
    Views:
    1,132
    Bart van Ingen Schenau
    Mar 16, 2010
Loading...

Share This Page