编译器添加到空类声明中是什么? [英] What does a compiler add to an empty class declaration?

查看:309
本文介绍了编译器添加到空类声明中是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我写

class A { };

编译器应提供(需要时)

The compiler should provide (as and when needed)



  1. 一个构造函数

  2. 一个析构函数

  3. >
  4. = operator


推荐答案

这是完整的。但有两点你应该注意:

It's complete. But there are two points you should note:


  1. 这是 copy =运算符。就像有一个复制构造函数,有一个复制赋值运算符。

  2. 仅在实际使用时提供。

  1. It's the copy =operator. Just like there is a copy constructor, there is a copy assignment operator.
  2. They are only provided if actually used.

2的一些解释:

struct A { private: A(); };
struct B : A { };

很好!提供一个默认的构造函数对于B是不成立的,因为它不能调用基类的构造函数。但是默认构造函数(和其他特殊函数)只提供(我们说它是隐式定义),如果它实际需要的话。

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.

这篇关于编译器添加到空类声明中是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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