c++ - 主体为空的类是不完整类型吗?

标签 c++ class declaration definition incomplete-type

类声明使该类成为不完整类型,因此无法定义该类的任何对象。不完整类型的定义是未指定其成员。这是否意味着一个具有空主体且在其定义中未指定成员的类会使其成为不完整类型?

class Empty { };

Empty e1; // okay, but why?

最佳答案

Is a class with empty body an incomplete type?

不,具有空主体的类只是一个空类,但仍然是一个完全定义的类。不完整类型是指其完整定义不可见的类型。

class Empty;

// Here, Empty is an incomplete type

class Empty { };

// Here, Empty is a complete type

根据 C++11 标准第 3.9/5 段:

A class that has been declared but not defined, or an array of unknown size or of incomplete element type, is an incompletely-defined object type. Incompletely-defined object types and the void types are incomplete types (3.9.1). Objects shall not be defined to have an incomplete type.

此外,根据第 9.2/2 段:

A class is considered a completely-defined object type (3.9) (or complete type) at the closing } of the class-specifier. [...]

关于c++ - 主体为空的类是不完整类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16573191/

相关文章:

c++ - 如何在Qt中将json数据附加到现有的json文件中

c++ - 添加用户定义类型 C++

c++ - 使用 ifstream 读取大型二进制文件不会填充字符缓冲区 C++

FA 中的 C++ "call of overloaded is ambiguous"

c++ - 如何使用 cmake 从 Visual Studio 构建 ia32 解决方案

C++ - 成员对象中枚举的行为

android - 我怎么 "Open the MainActivity class and add the corresponding method:"

C++ Java static final 等价物

c++ - `template<typename>;` 是合法的 C++ 吗?

c - 在 C 中,如果在 block 范围内声明的对象没有链接,为什么没有 "extern"的 main() 中的函数声明有效?