关于包含不可复制成员引用的类的复制构造函数的建议
我有一个 A 类,它作为成员引用了 B 类的对象。 B 类的复制构造函数(和赋值运算符)是私有的。您认为这是一个有效的好主意吗? A 的默认复制构造函数。(我实际上想要一个功能,可以在某种 STL 容器中存储类型 A 的大量对象,这需要分配能力和复制能力。)
class A
{
private:
B& b_;
public:
A(B& b) : b_(b){}
}
到目前为止,据我所知对上述方法的反对意见如下,但我的设计没有面对它。我想知道上面的例子是否还有其他问题/问题/担忧...
- 仅复制引用,因此,当 B 类型的原始对象 b 被销毁时,会出现问题。 (不适用,因为 b 在整个范围内可用。)
- b_ 对于 A 的每个实例都是唯一的吗? (不,B 实际上只在作用域中实例化一次,因此它具有单例类的效果。)
如果还有其他问题,请在此处列出。我不热衷于明确定义的复制构造函数,但我对此保持开放的态度。
I have a class A which has an reference to an object of class B as a member. The copy constructor (and assignment operator) of class B is private. Do you think it is a valid and good idea to use
the default copy constructor for A. (I actually want a functionality where I can store a lots of object for type A in some sort of an STL Container which requires both assign-ability and copy-ability.)
class A
{
private:
B& b_;
public:
A(B& b) : b_(b){}
}
Till now, in my knowledge the objections to above method are the following but my design does not face it. I would like to know if there are some other problems/issues/concerns about the above example...
- Only the reference is copied and hence, there would be problems when the original object b of type B is destroyed. (Does not apply, because b is available throughout the entire scope.)
- Is b_ unique for every instance of A? (No, B is actually instantiated only once in the scope, so it has the effect of a singleton class.)
If there are other concerns, please list them here. I am not keen on a explicitly defined copy-constructor but i'm keeping an open mind towards it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
作为一般准则,我从不将引用存储在对象内,因为我无法免费获得复制语义。
我改为存储指针。在这里,存储一个哑指针并让编译器为您实现复制语义似乎很好:
使用指针具有一定的灵活性:例如,默认构造可以将指针设置为零,后续操作检查其有效性(或简单地
断言
)。此外,指针可以重置,而引用则不然。As a general guideline, I never store references inside objects because I cannot get copy semantics for free.
I store pointers instead. Here, storing a dumb pointer and letting the compiler implement copy semantics for you seems fine:
Using a pointer has some flexibility: for instance, default construction can set the pointer to zero, and subsequent operations check against its validity (or simply
assert
). Also, the pointer can be reset, which is not the case with a reference.检查三法则。
如果您需要重载其中任何一个(析构函数、复制构造函数和复制赋值运算符),则需要重载所有这三个函数。如果您不重载其中任何一个,那么您可以依赖编译器生成的默认函数。
Check the Rule of Three.
If you need to overload any one of these(destructor, copy constructor & copy assignment operator) three you need to overload all of them. If you don't overload any of these then you can rely on the default functions the compiler generates.
您没有按值存储
B
并且您了解生命周期问题(无论是否复制它它们都适用),所以我认为使用默认的复制构造函数很好。作为旁注,我建议将
A(B&)
构造函数显式设置,以避免隐式地将B
视为A
。You aren't storing a
B
by value and you understand the lifetime issues (they apply regardless if copying it around) so I think it's fine to use the default copy constructor.As a side note I do recommend making the
A(B&)
constructor explicit to avoid implicitly treating aB
as anA
.如果 B 的赋值运算符是私有的,则编译器无法为 A 生成默认的赋值运算符。您必须显式声明赋值运算符。否则,您会收到编译错误:http://msdn.microsoft.com/en-us/library/aa983787%28v=vs.71%29.aspx
完成后那么,将对象 A 放入 STL 容器中应该没有问题。
If assignment operator of B is private, then the compiler cannot generated a default a assignemnt operator for A. You will have to declare a assignment operator explicitly. Otherwise you got a compile error : http://msdn.microsoft.com/en-us/library/aa983787%28v=vs.71%29.aspx
Once you done that, you should have no problem put object A into STL container.