c++ - unique_ptr 在 C++ 中的用法

标签 c++ unique-ptr

<分区>

如何在下面的代码中传递 unique_ptr?此代码无法编译。

int abc(int*& a)
{  
    return 1;
}

int main()
{
    std::cout<<"Hello World";
    std::unique_ptr<int> a(new int); 
    abc(a.get());
    return 0;
}

最佳答案

您可以通过 const 引用或引用来传递它,例如:

int abc(const std::unique_ptr<int>& a)

关于c++ - unique_ptr 在 C++ 中的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55843800/

相关文章:

c++ - 实例化和删除对象 C++

c++ - 为什么这不是 C++ 中的内存泄漏?

c++ - 使用 std::vector 的 std::unique_ptr

c++ - 无法理解如何将新对象添加到 std::list<std::unique_ptr<classname>>

c++ typeid 使用 get() 和 * 为相同的 unique_ptr 返回不同的值

c++ - 我怎样才能从OpenCV的凸缺陷点画出最小的圆

c++ - 交叉数据结构

c++ - boost::compute 是否支持 const 修饰符?

c++ - 如何申报 SFINAE 类(class)?

c++ - 带有 unique_ptr 的前向声明?