当然,我们甚至不应该考虑做这样的事情,我知道,但这仍然很有趣:
class A; //declaration
struct A {...}; //definition
struct B; //declaration
class B {...}; //definition
当我考虑它时,如果真的允许这样的事情,我没有看到任何问题(因为结构和类本质上是同一件事)。但它是(标准的)吗?
MSVC 接受并编译它,但会发出警告。
最佳答案
根据标准它是允许的,但正如一些编译器警告的那样,它不是很有用。
我相信这个警告是/是由 MSVC 对结构和类使用不同的名称修饰引起的,这会使它变得更没用......
根据@Armen 的要求:
7.1.5.3 详尽的类型说明符,p3
... in any elaborated-type-specifier, the
enum
keyword shall be used to refer to an enumeration (7.2), theunion
class-key shall be used to refer to a union (clause 9), and either theclass
orstruct
class-key shall be used to refer to a class (clause 9), declared using theclass
orstruct
class-key.
关于c++ - 使用 struct 关键字声明一个类,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6485419/