[英]Which is a better way - store `const reference` vs `boost::shared_ptr<Class>` as a member variable
class ClassOne : boost::noncopyable
{
};
class ClassTwo : boost::noncopyable
{
public:
ClassTwo(const ClassOne& one)
: m_one ( one ) {}
private:
const ClassOne& m_one;
};
class ClassThree : private boost::noncopyable
{
public:
ClassThree(boost::shared_ptr<const ClassOne> one)
: m_one ( one ) {}
private:
boost::shared_ptr<const ClassOne> m_one;
};
class ClassFour : private boost::noncopyable
{
public:
ClassFour(const boost::shared_ptr<const ClassOne>& one)
: m_one ( one ) {}
private:
boost::shared_ptr<const ClassOne> m_one;
};
Question> During codereview, I was told that the code (similar as ClassTwo
) should be replaced by the code (similar as ClassThree
) because storing a const reference to outside class is NOT safe. 问题>在代码查看期间,我被告知代码(类似于
ClassTwo
)应该被代码替换(类似于ClassThree
),因为将const引用存储到外部类是不安全的。
Is that true? 真的吗?
Thank you 谢谢
Const reference and shared_ptr
models two similar, but different concepts. Const引用和
shared_ptr
模型有两个相似但不同的概念。
If you have a const reference, you "know" something, you can inspect this thing (through const methods) but you can't change this thing, and even more important, this thing might vanish any time: you don't own it. 如果你有一个const引用,你“知道”某些东西,你可以检查这个东西(通过const方法),但你不能改变这个东西,更重要的是,这个东西可能随时消失:你不拥有它。
On the other hand, shared_ptr
models a shared ownership . 另一方面,
shared_ptr
为共享所有权建模。 You own the object pointed by the pointer. 您拥有指针指向的对象。 You can change it and it wont be destructed, unless every owner gets destructed.
你可以改变它,它不会被破坏,除非每个主人都被毁坏。
Returning const reference to a private member is safe; 将const引用返回给私有成员是安全的; accepting such a reference is a different thing.
接受这样的参考是另一回事。 You have to make sure the reference remains valid.
您必须确保引用仍然有效。
shared_ptr
is easier to handle but it's a more expensive solution. shared_ptr
更容易处理,但它是一个更昂贵的解决方案。
Regarding the exact dynamics, read the manual of shared_ptr
关于确切的动态,请阅读
shared_ptr
的手册
I think erenon has a good writeup. 我认为erenon写得很好。
I'd like to add a little from the pragmatic angle: 我想从务实的角度补充一点:
const reference members make classes non-copyable (in fact, they could be copyable, but generation of default special members is inhibited (except for the destructor) const引用成员使类不可复制(事实上,它们可以是可复制的,但是禁止生成默认的特殊成员(析构函数除外)
shared_ptr, on the other hand, make stuff inherently copyable (with shallow-clone semantics). 另一方面,shared_ptr使内容本身可复制(使用浅克隆语义)。 This turns out to be really useful in functors where state will be kept/passed along.
事实证明,在状态将被保留/传递的仿函数中非常有用。 Boost Asio is a prime example, because logical threads of execution meander across threads and the lifetime is largely unpredictable.
Boost Asio是一个很好的例子,因为执行的逻辑线程在线程之间蜿蜒而且生命周期在很大程度上是不可预测的。
I suggest using a shared_ptr<const T>
; 我建议使用
shared_ptr<const T>
; This adds immutability in the context of sharing . 这增加了共享背景下的不变性 。 You will need to clone the object pointed to replace it with a changed version, and the shared object will not be modified through the
shared_ptr<const T>
您将需要克隆指向的对象以使用更改的版本替换它,并且不会通过
shared_ptr<const T>
修改共享对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.