class a{
4 public:
5 a(char i):x(i){type = char_t;}
6 a(int i):y(i){type = int_t;}
7 a(long i):z(i){type = long_t;}
8 // private:
9 enum dog{char_t,int_t,long_t}type;
10 void print();
11 private:
12 union {char x;int y;long z;};//匿名联合体
13 };
14 void a::print(){
15 switch(type){
16 case char_t:
17 cout << x <<'\t' << "char" << endl;
18 break;
19 case int_t:
20 cout << y << '\t' << "int" << endl;
21 break;
22 case long_t:
23 cout << z << '\t' << "long" << endl;
24 }
25
void f2(){
38 union abc{char i;int j;long k;};//最后的有一个分号
39 abc a;
40 abc b;
41 abc c;
42 a.i = 'a';
43 b.j = 1099;
44 c.k = 12321;
45 }
不是匿名的union
void f2(){
17 enum chy{a,b,c,d};
18 cout << sizeof(chy) << endl;
19 enum ch{x,y,z};
20 cout << sizeof(ch) << endl;
21 }
结果都是4