How do I pass smart pointers into functions?

后端 未结 3 1611

When passing objects into functions, do the same rules apply to smart pointers as to other objects that contain dynamic memory?

When I pass, for example, a std::

相关标签:
3条回答
  • 2021-01-30 18:39

    If the function isn't going to modify or make a copy of the pointer, just use a dumb pointer instead. Smart pointers are used to control the lifetime of an object, but the function isn't going to change the lifetime so it doesn't need a smart pointer, and using a dumb pointer gives you some flexibility in the type used by the caller.

    void function(std::string * ptr);
    
    function(my_unique_ptr.get());
    function(my_shared_ptr.get());
    function(my_dumb_ptr);
    

    unique_ptr can't be copied, so if you must pass it you must pass a reference.

    0 讨论(0)
  • 2021-01-30 18:48

    Smart pointers have pointer semantics, not value semantics (well, not the way you mean it). Think of shared_ptr<T> as a T*; treat it as such (well, except for the reference counting and automatic deletion). Copying a smart pointer does not copy the object it points to, just like copying a T* does not copy the T it points to.

    You can't copy a unique_ptr at all. The whole point of the class is that it cannot be copied; if it could, then it wouldn't be a unique (ie: singular) pointer to an object. You have to either pass it by some form of reference or by moving it.

    Smart pointers are all about ownership of what they point to. Who owns this memory and who will be responsible for deleting it. unique_ptr represents unique ownership: exactly one piece of code owns this memory. You can transfer ownership (via move), but in so doing, you lose ownership of the memory. shared_ptr represents shared ownership.

    In all cases, the use of a smart pointer in a parameter list represents transferring ownership. Therefore, if a function takes a smart pointer, then it is going to claim ownership of that object. If a function isn't supposed to take ownership, then it shouldn't be taking a smart pointer at all; use a reference (T&) or if you have need of nullability, a pointer but never store it.

    If you are passing someone a unique_ptr, you are giving them ownership. Which means, by the nature of unique ownership, you are losing ownership of the memory. Thus, there's almost no reason to ever pass a unique_ptr by anything except by value.

    Similarly, if you want to share ownership of some object, you pass in a shared_ptr. Whether you do it by reference or by value is up to you. Since you're sharing ownership, it's going to make a copy anyway (presumably), so you might as well take it by value. The function can use std::move to move it into class members or the like.

    0 讨论(0)
  • 2021-01-30 19:04

    A smart pointer is an object that refer to another object an manages its lifetime.

    Passing a smart pointer reuquires to respect the semantics the smart poitner support:

    • Passing as const smartptr<T>& always work (and you cannot change the pointer, but can change the state of what it points to).
    • Passing as smartptr<T>& always work (and you can change the pointer as well).
    • Passing as smartptr<T> (by copy) works only if smartptr is copyable. It works with std::shared_ptr, but not with std::unique_ptr, unless you "move" it on call, like in func(atd::move(myptr)), thus nullifying myptr, moving the pointer to the passed parameter. (Note that move is implicit if myptr is temporary).
    • Passing as smartptr<T>&& (by move) imposes the pointer to be moved on call, by forcing you to explicitly use std::move (but requires "move" to make sense for the particular pointer).
    0 讨论(0)
提交回复
热议问题