C++ 默认复制构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12577907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
default copy constructor
提问by sourabh912
Can the (implicit)defaultcopy constructorbe called for a classthat has already user-defined constructorbut that is not the copy constructor?
可以为已经具有用户定义构造函数但不是复制构造函数的类调用(隐式)默认复制构造函数吗?
If it is possible then, suppose we define the copy constructor for the class explicitly, now can the (implicit)default constructor be called?
如果可能的话,假设我们为类显式定义了复制构造函数,现在可以调用(隐式)默认构造函数吗?
回答by James Kanze
First, let's clarify our vocabulary a bit. A default constructor is a constructor which can be called without any arguments. A copy constructor is a constructor which can be called with a single argument of the same type. Given this, a "default copy constructor" would be a constructor with a signature something like:
首先,让我们稍微澄清一下我们的词汇。默认构造函数是可以在没有任何参数的情况下调用的构造函数。复制构造函数是可以使用相同类型的单个参数调用的构造函数。鉴于此,“默认复制构造函数”将是具有如下签名的构造函数:
class MyClass
{
public:
static MyClass ourDefaultInstance;
// default copy constructor...
MyClass( MyClass const& other = ourDefaultInstance );
};
Somehow, I don't think that this is what you meant. I thinkwhat you're asking about is an implicitly declared or an implicitly defined copy constructor; a copy constructor whose declaration or definition is provided implicitly by the compiler. The compiler will always provide the declaration unless you provide a declaration of something that can be considered a copy constructor. Providing other constructors will not prevent the compiler from implicitly declaring a copy constructor.
不知何故,我不认为这就是你的意思。我认为您要问的是隐式声明或隐式定义的复制构造函数;一个复制构造函数,其声明或定义由编译器隐式提供。编译器将始终提供声明,除非您提供可被视为复制构造函数的声明。提供其他构造函数不会阻止编译器隐式声明复制构造函数。
This is different from the default constructor—anyuser defined constructor will prevent the compiler from implicitly declaring a default constructor. This means that if you have a user defined copy constructor, the compiler will not implicitly declare a default constructor.
这与默认构造函数不同——任何用户定义的构造函数都会阻止编译器隐式声明默认构造函数。这意味着如果您有用户定义的复制构造函数,编译器将不会隐式声明默认构造函数。
The second important point is that you do not call constructors. The compiler calls them in certain well defined contexts: variable definition and type conversion, mainly. The compiler can only call constructors that are declared (including those that are implicitly declared). So if you have a user defined constructor (copy or otherwise), and do not define a default constructor, the compiler cannot call the constructor except in contexts where it has arguments to call it with.
第二个重点是你不调用构造函数。编译器在某些明确定义的上下文中调用它们:主要是变量定义和类型转换。编译器只能调用已声明的构造函数(包括隐式声明的构造函数)。因此,如果您有一个用户定义的构造函数(复制或其他方式),并且没有定义默认构造函数,则编译器不能调用该构造函数,除非在它有调用它的参数的上下文中。
To summarize what I think your questions are: the compiler will provide an implicit copy constructor even if the class has other user defined constructors, provided none of those constructors can be considered copy constructors. And if you provide a user defined copy constructor, the compiler will notprovide an implicitly declared default copy constructor.
总结一下我认为您的问题是:即使类具有其他用户定义的构造函数,编译器也会提供隐式复制构造函数,前提是这些构造函数都不能被视为复制构造函数。如果您提供用户定义的复制构造函数,编译器将不会提供隐式声明的默认复制构造函数。
回答by CrazyCasta
http://www.cplusplus.com/articles/y8hv0pDG/
http://www.cplusplus.com/articles/y8hv0pDG/
The default copy constructor exists if you have not defined one. So yes you can call the default copy constructor, if you haven't defined a copy constructor, however if you do define a copy constructor in your class, you will not be able to call the default one.
如果您尚未定义,则默认复制构造函数存在。所以是的,你可以调用默认的复制构造函数,如果你没有定义一个复制构造函数,但是如果你在你的类中定义了一个复制构造函数,你将无法调用默认的。
回答by NullPoiиteя
There is no such thing as a default copy constructor. There are default constructors and copy constructors and they are different things.
没有默认复制构造函数这样的东西。有默认构造函数和复制构造函数,它们是不同的东西。
The implicitly defined copy constructor (which I think is what you mean by "default copy constructor") will copy non-static members of class type using their copy constructor, not their default constructor. The implicitly defined copy constructor is used when you don't define your own copy constructor.
隐式定义的复制构造函数(我认为这就是“默认复制构造函数”的意思)将使用它们的复制构造函数复制类类型的非静态成员,而不是它们的默认构造函数。当您没有定义自己的复制构造函数时,将使用隐式定义的复制构造函数。