CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: auto_ptr

  1. #1
    Join Date
    Jul 2002
    Posts
    788

    auto_ptr

    Hi,

    I have a few questions regarding this auto_ptr

    1) I should only use auto_ptr within a function??

    2) Can i use auto_ptr<int> or auto_ptr<SomeClass> in a class?
    example:
    Code:
    class Test
    {
    private:
    auto_ptr<int> ptrData;
    }
    If not why?

    3)auto_ptr<std::vector> vec1 for example, why is it after copying such as
    auto_ptr<std::vecotr> vec2(vec1), vec2 becomes unmodifiable? how to get around this?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: auto_ptr

    Quote Originally Posted by mce
    3)auto_ptr<std::vector> vec1 for example, why is it after copying such as
    auto_ptr<std::vecotr> vec2(vec1), vec2 becomes unmodifiable? how to get around this?
    You probably mean to say that vec1 no longer owns the vector. Basically, auto_ptr has unusual copy semantics...

    Quote Originally Posted by mce
    2) Can i use auto_ptr<int> or auto_ptr<SomeClass> in a class?
    ... so if you have an auto_ptr member in a class, and you don't disable copying, you'll have to worry about the unusual copy semantics that objects of your class will have...

    Quote Originally Posted by mce
    1) I should only use auto_ptr within a function??
    ... hence using an auto_ptr in the way you would use Boost's scoped_ptr is a way to keep things simple.

    Note that the 2011 version of the C++ standard introduces std::unique_ptr and deprecates std::auto_ptr (because of the problems that it can cause, and the fact that unique_ptr can replace it for what it is useful for).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured