gpt4 book ai didi

c++ - C++中构造函数的默认访问是什么

转载 作者:太空狗 更新时间:2023-10-29 19:37:23 24 4
gpt4 key购买 nike

什么在c++中构造函数的默认访问是什么?为什么

公共(public)的、私有(private)的还是 protected ?

我如何通过代码检查它?

最佳答案

如果您不自己声明构造函数,编译器将始终为您生成一个public 普通构造函数。它们还将隐式创建公共(public)复制构造函数和复制赋值运算符。

来自 C++ 标准 12.1.5:

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:

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:

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,则不会始终生成移动构造函数。有关详细信息,请参阅第 12.8.20 节。

关于c++ - C++中构造函数的默认访问是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32235294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com