什么是C ++中的构造函数的默认访问权限 [英] What is the default access of constructor in c++
问题描述
什么是c ++中构造函数的默认访问权限,为什么?
What is the default access of constructor in c++ and why ?
公共,私有或受保护?
public, private or protected ?
如何通过 code 检查它?
推荐答案
如果您自己没有声明构造函数,则编译器将始终为您生成一个 public
琐碎的代码。他们还将隐式创建一个公共副本构造函数和副本赋值运算符。
If you do not declare a constructor yourself, the compiler will always generate a public
trivial one for you. They will also implicitly create a public copy constuctor and copy assignment operator.
从c ++标准12.1.5开始:
From c++ standard 12.1.5:
如果类X没有用户声明的构造函数,则将不带参数的构造函数隐式声明为默认值。隐式声明的默认构造函数是其类的内联公共成员。
If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted. An implicitly-declared default constructor is an inline public member of its class.
12.8.7和12.8.11:
12.8.7 and 12.8.11:
如果类定义未显式声明一个副本构造函数,则隐式声明一个副本构造函数。 [...]
隐式声明的复制/移动构造函数是其类的内联公共成员。
If the class definition does not explicitly declare a copy constructor, one is declared implicitly. [...] An implicitly-declared copy/move constructor is an inline public member of its class.
最后12.8.18、12.8.20、12.8.22:
Finally 12.8.18, 12.8.20, 12.8.22:
如果类定义未明确声明一个副本赋值运算符,则声明一个隐含地。 [...]
如果类X的定义未明确声明移动赋值运算符,则将隐式声明[...]。
隐式声明的复制/移动赋值运算符是其类的嵌入式公共成员。
If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly. [...] If the definition of a class X does not explicitly declare a move assignment operator, one will be implicitly declared [...]. An implicitly-declared copy/move assignment operator is an inline public member of its class.
如果使用的 c ++ 11
不会总是生成move构造函数。有关更多信息,请参见12.8.20节。
If you are using c++11
the move constructor will not always be generated. See section 12.8.20 for more information.
这篇关于什么是C ++中的构造函数的默认访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!