【发布时间】:2018-05-21 02:15:18
【问题描述】:
使用 std::shared_ptr 和 & 来保存对对象的引用有什么区别?
class A{};
class B
{
private:
std::shared_ptr<A>&obj; // or std::shared_ptr<A>obj;
public:
B(std::shared_ptr<A>r) : obj(r) {}
};
int main()
{
std::shared_ptr<A>a(new A());
std::unique_ptr<B>b(new B(a));
std::cin.get();
return 0;
}
【问题讨论】:
-
我想知道使用智能指针是否比取消引用对象(作为指针传递)将 obj 保存为普通引用更好
标签: c++ shared-ptr