私有复制构造函数:不允许
我正在一个开源库中进行编程,该库的代码中几乎没有注释,并且绝对没有与代码相关的文档,或者此类注释绝对没有显示任何内容或完全令人困惑。库的示例类有时定义如下(这是一个抽象的简短示例):
class A
{
private:
// Disallow default bitwise copy construct.
A (const A& Acopy) { data = Acopy.data; };
int data;
public:
A() {};
A (int arg) : data(arg) {};
A(const A& Acopy) { data = Acopy.data; };
};
私有复制构造函数前面的注释“Dissallow默认按位复制构造”将指出以下事实:我定义了一种类型,我需要定义自己的复制构造函数,以避免编译器为我模糊地“生成”一个类型。这是我迄今为止在这个主题上学到的知识。但在这种情况下,构造函数是私有,并且编译会以这种形式中断。
问:这样的事情有原因吗?私有的复制构造函数?这个评论意味着什么?
托米斯拉夫
I'm programming in an open source library which has very little comments in the code, and absolutely no code related documentation, or such comments which show absolutely nothing or are completely confusing. An example class of the library is sometimes defined as follows (this is an abstracted short example):
class A
{
private:
// Disallow default bitwise copy construct.
A (const A& Acopy) { data = Acopy.data; };
int data;
public:
A() {};
A (int arg) : data(arg) {};
A(const A& Acopy) { data = Acopy.data; };
};
The comment "Dissalow default bitwise copy construct" in front of a private copy constructor would point to the fact that when I define a type I need to define my own copy constructor to avoid one being "generated" for me ambiguously by the compiler. This is what I have learned so far on this topic. But in this case, the constructor is private, and the compilation breaks in this form.
Q: Is there a reason for such thing? A copy constructor that is private? And what could this comment mean?
Tomislav
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
和你说的意思差不多。通常,编译器会生成一个复制构造函数。为了防止这种情况,您可以定义自己的,并将其设置为私有。那么任何复制构造类的尝试都将在编译时失败,而不是默默地做错误的事情。
It means pretty much what you said. Normally, the compiler would generate a copy constructor. To prevent this, you can define your own, and make it private. Then any attempt at copy-constructing the class will fail at compile-time, rather than silently doing the wrong thing.
通常,复制构造函数被设置为私有,以禁止按值传递对象。
Usually, A copy constructor is made private to disallow Passing of objects by value.
我认为编译会中断,因为复制构造函数被定义了两次,一次是私有的,一次是公共的。
使用私有复制构造函数的原因可能是防止 A 的实例按值传递或返回。为什么要这样做是另一回事,我无法回答。
The compilation breaks, I think, because the copy constructor is defined twice, once as private and once as public.
A reason for a private copy constructor might be to prevent an instance of A being passed or returned by value. Why one would want to do this is another matter, which I can't answer.
正如您所说,重点是避免生成默认的复制构造函数,但不仅如此 - 它是
private
以避免实际使用它。将其设为私有的目的是不允许使用它。The point is, as you said, to avoid generating default copy constructors, but not only that - it's
private
to avoid actually using it. The point of making it private is not to allow using it.编译会中断,因为您有两个复制构造函数,一个是公共的,一个是私有的。私有复制构造函数完全没问题。他们不允许用户做许多危险的事情。
Compilation breaks because you have two copy constructors, one public and one private. Private copy constrcutors are perfectly alright. They disallows many dangerous things that the user can do.