assignment to auto_ptr

This is a discussion on assignment to auto_ptr within the C++ Programming forums, part of the General Programming Boards category; Hi, I am learning to use auto_ptr. my question is in purple comments below. Code: #include <iostream> using namespace std; ...

  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
    21,725
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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,893
    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,796
    >Why?
    It's shorter and more directly reflects your intentions?
    My best code is written with the delete key.

  8. #8
    ZuK
    ZuK is offline
    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
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Is it the same with boost::shared_ptr regarding .reset() thing?

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    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, 08:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21