2

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:

  1. Is the pointer copied when passed into UsePointer function? Hence the owernship is transferred?
  2. 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?

Lost1
  • 990
  • 1
  • 14
  • 34
  • 4
    Forget that `std::auto_ptr` ever existed. It's just bad. – Yksisarvinen Jul 28 '20 at 09:02
  • @Yksisarvinen does std::unique_ptr behave the same way if we change auto_ptr to unique_ptr? – Lost1 Jul 28 '20 at 09:10
  • Whether you are using auto_ptr or unique_ptr, there is no reason to pass it as such to the function if you are not changing the reference count. Just passing the raw pointer is more effective. See also https://herbsutter.com/2013/06/05/gotw-91-solution-smart-pointer-parameters/ and yes, I agree with @Yksisarvinen, don't use auto_ptr – AndersK Jul 28 '20 at 09:14
  • Regarding your question - `unique_ptr` wouldn't compile snd report and error instead. `unique_ptr` as its name stands is "unique" and there must not be two unique_ptr to the same object. `shared_ptr` has reference counter and it can be copied. – ALX23z Jul 28 '20 at 11:40

1 Answers1

4

Is the pointer copied when passed into UsePointer function? Hence the owernship is transferred?

Yes. Unless the function parameter is reference qualified, arguments pass by value. For auto_ptr that involves copying, and thus passing ownership.

What do I need to if want spObject not be destroyed? Do I need to pass this pointer by reference?

You could. But better yet, pass a reference to the object itself. A function shouldn't accept smart pointers unless manipulation of the ownership is involved. If it just needs to do something with pointee, accept a CSomeClass const& and pass *spObject.

Also this book is a bit outdated - does the same hold for unique_ptr in c++ 11?

Yes. With the difference that unique_ptr is not copyable, and so cannot pass its ownership away implicitly. To pass a unique_ptr, it must be moved explicitly. The appearance of std:move in the code that passes the pointer into the function gives an explicit visual cue that ownership changes.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • I don't 100% understand the last bit - what happens if we have "void UsePointer (auto_ptr spObj)" - is the object destroyed? (or is this use case completely forbidden because) – Lost1 Jul 28 '20 at 09:17
  • @Lost1 - I'm not sure I understand you. The function parameter `spObj` exists only during the function call. When the function returns, the parameter is destroyed. If the parameter was initialized to own a pointer to another object, then that object gets destroyed too along with the parameter. That's the same for both `auto_ptr` and `unique_ptr`. The only difference is in how they can get initialized. – StoryTeller - Unslander Monica Jul 28 '20 at 09:24
  • What I meant is that - is "void UsePointer (unique_ptr spObj)" disallowed because unique_ptr cannot be copied? – Lost1 Jul 28 '20 at 09:26
  • I guess https://stackoverflow.com/questions/30905487/how-can-i-pass-stdunique-ptr-into-a-function answers my question? – Lost1 Jul 28 '20 at 09:27
  • @Lost1 - Of course it's not disallowed. You can declare the function and call it just fine. Only difference is that to construct the function parameter you'd need to pass in `UsePointer(std::move(spObject));` – StoryTeller - Unslander Monica Jul 28 '20 at 09:28
  • Yes. I understand this now - cannot pass in the unique_ptr directly. – Lost1 Jul 28 '20 at 09:31