From: Ulrich Eckhardt on 12 Apr 2010 08:06 Jack wrote: > Does that mean objects declared with std::auto_ptr only live once and > simply be gone and can't be reused? A std::auto_ptr instance is an object like any other object, too, there are no exceptions to it. That said, auto_ptrs are used to ensure _exclusive_ ownership (i.e. ownership by exactly one) for the pointee (the one it points to). Whether that fits your design is up to you to decide. Uli -- C++ FAQ: http://parashift.com/c++-faq-lite Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
From: Alex Blekhman on 12 Apr 2010 10:02 On 12-Apr-10 14:40, Jack wrote: > Does that mean objects declared with std::auto_ptr only live once and simply > be gone and can't be reused? If so, I catch your points. In addition to Ulrich's reply. Yes and no. You can always give pointer ownership with auto_ptr::release call. However, the main advantage of auto_ptr is to call `delete' automatically on the stored pointer when auto_ptr instance goes out of scope. That's why it is called auto_ptr ("automatic pointer") to begin with. Sometimes this is exactly what you want: void foo() { std::auto_ptr<MyClass> ptrMy(new MyClass); // use ptrMy as regular poiner ptrMy->Bar(); ... // No need to call `delete', std::auto_ptr will take care of // the pointer upon regular exit or exit via exception. } HTH Alex
First
|
Prev
|
Pages: 1 2 Prev: Static linking to MFC/CRT and Standard C++ libraries Next: using multiple resource file |