默认复制构造函数 [英] default copy constructor

查看:284
本文介绍了默认复制构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以为已经是用户定义的构造函数但不是复制构造函数的类调用默认(隐式)复制构造函数。如果可能的话,假设我们为类定义了显式的复制构造函数,现在可以调用默认(隐式)构造函数。

Can the default(implicit) 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 default(implicit) 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.

第二个重点是你不调用构造函数。
编译器在某些定义良好的上下文中调用它们:variable
主要是定义和类型转换。编译器只能调用
声明的构造函数(包括隐式声明的
)。所以如果你有一个用户定义的构造函数(否则为copy或
),并且不定义一个默认构造函数,编译器不能调用构造函数,除非在上下文中有参数调用
it 。

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.

总结我认为你的问题是:编译器将提供
一个隐式拷贝构造函数,即使该类有其他用户定义
构造函数,只要这些构造函数都不能被认为是copy
构造函数。如果你提供一个用户定义的复制构造函数,
编译器将不会提供一个默认的默认构造函数。

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 constructor.

这篇关于默认复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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