gpt4 book ai didi

c++ - unique_ptr : So I can't reference to a deleted function anymore

转载 作者:行者123 更新时间:2023-11-28 00:18:20 25 4
gpt4 key购买 nike

我有 2 个类,我想返回对私有(private)成员对象的引用。

class BB{};

class B
{
std::unique_ptr<BB> b;
public:
const std::unique_ptr<BB>& getBB(){return b;}
};
int main()
{
B b;
std::unique_ptr<BB> x=b.getBB();
}

毫无疑问,错误发生在 main 处的 x=b.GetBB()...无法引用。这是一个被删除的函数。

最佳答案

您正在尝试复制初始化 unique_ptr,这是不允许的,因为 unique_ptr 已删除复制构造函数。尝试

const std::unique_ptr<BB>& x = b.getBB();

关于c++ - unique_ptr : So I can't reference to a deleted function anymore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28826350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com