如何鉴别C++中的struct被class化了?
C++引入了“域”的概念,使得作用域的语义得到扩张。
C++中的结构和类真的差不多了:
#include <stdio.h>
struct Test
{
struct Hello
{
int a;
};
union
{
int b;
char c;
};
};
void main(void)
{
struct Test t;
t.b = 10;
printf("The size is: %d\n", sizeof(t));
}
将以上代码分别在.C和.CPP源文件中进行编译和运行。
然后在main()函数中再分别添上这句:t.a = 100;
显然在CPP中是无法通过编译的。因为在C++中,a不是Test结构的成员,而是
Test::Hello的成员。而Test::Hello就像是嵌套在类中的数据类型那样,使得Test
仅仅是struct Hello的一个域。