Suppose that we have
void UsePointer (auto_ptr <CSomeClass> spObj)
{
spObj->DoSomething();
}
and we have a main function:
int main()
{
auto_ptr <CSomeClass> spObject (new CSomeClass ());
UsePointer (spObject);
// spObject->DoSomthing (); would be invalid.
}
The book says "the object was destroyed when UsePointer() returned, because variable spObj went out of scope, hence destroyed"
My question is:
- Is the pointer copied when passed into UsePointer function? Hence the owernship is transferred?
- What do I need to if want spObject not be destroyed? Do I need to pass this pointer by reference?
Also this book is a bit outdated - does the same hold for unique_ptr in c++ 11?