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

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

问题描述

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

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?

推荐答案

首先,让我们澄清一下我们的词汇.默认构造函数是可以不带任何参数调用的构造函数.复印件constructor 是一个可以用单个参数调用的构造函数同类型.鉴于此,默认复制构造函数"将是带有类似签名的构造函数:

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.

这不同于用户定义的默认构造函数—any构造函数将阻止编译器隐式声明默认构造函数.这意味着如果您有用户定义的副本构造函数,编译器不会隐式声明一个默认值构造函数.

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天全站免登陆