问题描述
是否可以为已经 用户定义的构造函数的类调用(隐式)默认 复制构造函数,但这不是副本构造函数吗?
Can the (implicit)default copy constructor be called for a class that has already user-defined constructor but 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?
推荐答案
首先,让我们澄清一下词汇.默认构造函数是可以不带任何参数调用的构造函数.复印件构造函数是可以使用单个参数调用的构造函数相同类型的.鉴于此,默认副本构造函数"将是签名类似的构造函数:
First, let's clarify our vocabulary a bit. A default constructor is aconstructor which can be called without any arguments. A copyconstructor is a constructor which can be called with a single argumentof the same type. Given this, a "default copy constructor" would be aconstructor 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 think whatyou're asking about is an implicitly declared or an implicitly definedcopy constructor; a copy constructor whose declaration or definition isprovided implicitly by the compiler. The compiler will always providethe declaration unless you provide a declaration of something that canbe considered a copy constructor. Providing other constructors will notprevent the compiler from implicitly declaring a copy constructor.
这与默认的构造方法任何用户定义构造函数将防止编译器隐式声明默认构造函数.这意味着如果您有用户定义的副本构造函数,编译器不会隐式声明默认值构造函数.
This is different from the default constructor—any user definedconstructor will prevent the compiler from implicitly declaring adefault constructor. This means that if you have a user defined copyconstructor, the compiler will not implicitly declare a defaultconstructor.
第二个要点是不要调用构造函数.这编译器在某些定义明确的上下文中调用它们:变量定义和类型转换,主要是.编译器只能调用声明的构造函数(包括隐式的构造函数)声明).因此,如果您有用户定义的构造函数(复制或否则),并且不定义默认构造函数,则编译器无法调用构造函数,除非上下文中有要调用的参数
The second important point is that you do not call constructors. Thecompiler calls them in certain well defined contexts: variabledefinition and type conversion, mainly. The compiler can only callconstructors that are declared (including those that are implicitlydeclared). So if you have a user defined constructor (copy orotherwise), and do not define a default constructor, the compiler cannotcall the constructor except in contexts where it has arguments to callit with.
总结一下,我认为您的问题是:编译器将提供隐式副本构造函数,即使该类具有其他用户定义构造函数,前提是这些构造函数中的任何一个都不能视为副本构造函数.而且,如果您提供了用户定义的副本构造函数,编译器将不提供隐式声明的默认副本构造函数.
To summarize what I think your questions are: the compiler will providean implicit copy constructor even if the class has other user definedconstructors, provided none of those constructors can be considered copyconstructors. And if you provide a user defined copy constructor, thecompiler will not provide an implicitly declared default copy constructor.
这篇关于我定义了一个非复制构造函数;复制构造函数仍会隐式定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!