编译器会在空类声明中添加什么?
假设,我写
class A { };
编译器应该提供(在需要时)
- 构造函数
- 析构函数
- 复制构造函数
- = 运算符
这是编译器提供的全部吗?这份名单是否有任何补充或删除?
Suppose, I write
class A { };
The compiler should provide (as and when needed)
- a constructor
- a destructor
- a copy constructor
- = operator
Is this all the compiler provides? Are there any additions or deletions to this list?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
完成了。但有两点需要注意:
对2的一些解释:
没关系!对于“B”来说,提供默认构造函数是不正确的,因为它无法调用基类的构造函数。但默认构造函数(以及其他特殊函数)仅在实际需要时才提供(我们说它是隐式定义)。
It's complete. But there are two points you should note:
Some explanation for 2:
That's fine! Providing a default constructor would be ill-formed for "B", because it would not be able to call the base-class' constructor. But the default constructor (and the other special functions) is only provided (we say it's implicitly defined) if it's actually needed.
从 C++11 开始,除了您列出的之外,还包括
From C++11 onwards, in addition to what you have listed
您的清单已完成。这就是它所添加的全部内容。
Your list is complete. This is all it is adding.
名单尚未完成…………
除了上面提到的四个属性之外,还有一个地址运算符(&)重载方法,它返回调用对象的地址,也是由编译器自动提供的。
The list is not completed............
In addition with the above mention FOUR properties , there is an address operator ( & ) overloaded method , that return the address of the invoking object , is also provided automatically by the compiler.
有五个属性:
构造
函数 复制构造
函数 析构
函数
赋值运算符 引用运算符(&) - 地址
There are five properties:
constructor
copy constructor
destructor
assignment operator
the reference operator(&) - the address