C ++:默认副本构造函数是否会受到其他构造函数和析构函数的影响? [英] C++: Is default copy constructor affected by presence of other constructors and destructor?

查看:100
本文介绍了C ++:默认副本构造函数是否会受到其他构造函数和析构函数的影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道,如果声明了任何构造函数(包括复制构造函数),没有隐式创建默认构造函数(不带参数的构造函数)。默认复制构造函数(执行对象的浅表复制的构造函数)会发生同样的情况吗?此外,析构函数的存在是否会对此造成影响?

As we know, if any constructor is declared (copy constructor included), default constructor (the one that takes no arguments) is not implicitly created. Does the same happen with a default copy constructor (the one that performs shallow copy of an object)? Also, does the presence of destructor affect this anyhow?

推荐答案

此处的答案正确但不完整。它们对于C ++ 98和C ++ 03是正确的。在C ++ 11中,如果已声明move构造函数或move赋值运算符,则不会获得副本构造函数。此外,如果您声明了拷贝分配运算符或析构函数,则不赞成使用隐式生成拷贝构造函数。 12.8 [class.copy]:

The answers here are correct but not complete. They are correct for C++98 and C++03. In C++11 you will not get a copy constructor if you have declared a move constructor or move assignment operator. Furthermore if you have declared a copy assignment operator or a destructor, the implicit generation of the copy constructor is deprecated. 12.8 [class.copy]:


如果类定义没有
显式声明复制构造函数,则
在那里不是用户声明的move
构造函数,并且没有
用户声明的move分配
运算符,副本构造函数被隐式声明为默认
($ 8.4)。 2)。如果该类具有
用户声明的副本分配运算符
或用户声明的析构函数,则不建议使用此类隐式声明

If the class definition does not explicitly declare a copy constructor, there is no user-declared move constructor, and there is no user-declared move assignment operator, a copy constructor is implicitly declared as defaulted (8.4.2). Such an implicit declaration is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor.

这篇关于C ++:默认副本构造函数是否会受到其他构造函数和析构函数的影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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