How can I pass std::unique_ptr into a function

前端 未结 7 1023
忘了有多久
忘了有多久 2020-12-02 07:07

How can I pass a std::unique_ptr into a function? Lets say I have the following class:

class A
{
public:
    A(int val)
    {
        _val = val         


        
相关标签:
7条回答
  • 2020-12-02 07:58

    You're passing it by value, which implies making a copy. That wouldn't be very unique, would it?

    You could move the value, but that implies passing ownership of the object and control of its lifetime to the function.

    If the lifetime of the object is guaranteed to exist over the lifetime of the call to MyFunc, just pass a raw pointer via ptr.get().

    0 讨论(0)
提交回复
热议问题