我定义了一个非复制构造函数;复制构造函数仍会隐式定义吗? [英] I defined a non-copy constructor; will a copy constructor still be implicitly defined?

查看:40
本文介绍了我定义了一个非复制构造函数;复制构造函数仍会隐式定义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为已经 用户定义的构造函数的调用(隐式)默认 复制构造函数,但这不是副本构造函数吗?

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 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 think what 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—any user 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 not provide an implicitly declared default copy constructor.

这篇关于我定义了一个非复制构造函数;复制构造函数仍会隐式定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆