How can I pass std::unique_ptr into a function

前端 未结 7 1043
忘了有多久
忘了有多久 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:48

    Since unique_ptr is for unique ownership, if you want to pass it as argument try

    MyFunc(move(ptr));
    

    But after that the state of ptr in main will be nullptr.

提交回复
热议问题