Thread: assignment to auto_ptr

  1. #1
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116

    assignment to auto_ptr

    Hi,
    I am learning to use auto_ptr.
    my question is in purple comments below.

    Code:
    #include <iostream>
    using namespace std;
    
    class Base
    {
    public:
        virtual void print()=0;
    };
    
    class A : public Base
    {
    public:
        void print() { cout << "A" << endl; }
    };
    
    class B : public Base
    {
    public:
        void print() { cout << "B" << endl; }
    };
    
    int main()
    {
        auto_ptr<Base> owner;
        
        if(5==5)
        {
            A* a = new A;
            owner = a;    // this was my first guess, does not compile though.
            owner = auto_ptr<Base>(a); // this compiles. is this how you do it?
        }
        else
        {
            B* b = new B;
            owner = auto_ptr<Base>(b);
        }
    
        owner->print();
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, the second way is correct. You could just write:
    Code:
    owner = auto_ptr<Base>(new A);
    Incidentally, I believe you should #include <memory> for std::auto_ptr.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Thanks laserlight.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You should do
    Code:
    owner.reset(new A);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Quote Originally Posted by CornedBee View Post
    You should do
    Code:
    owner.reset(new A);
    Why? isn't it the same?

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, it avoids constructing a new auto_ptr and then copy-assigning it into the current one. Now, compilers might be able to optimize the additional copy away - but then, they might not.

    In any case, the reset() is the most common way of setting a smart pointer to a new value.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why?
    It's shorter and more directly reflects your intentions?
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by kroiz View Post
    Why? isn't it the same?
    not exactly
    Code:
    owner = auto_ptr<Base>(new A);
    creates an unnecessary temporary auto_ptr that is then assigned to owner.
    Kurt

  9. #9
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Understood.
    Thanks you all.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    630
    Is it the same with boost::shared_ptr regarding .reset() thing?

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes. reset() allows smart pointers to take ownership of an object by swapping it into its this pointer. That's all reset does, and you should use it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM