Chinaunix首页 | 论坛 | 博客
  • 博客访问: 532188
  • 博文数量: 104
  • 博客积分: 2089
  • 博客等级: 大尉
  • 技术积分: 1691
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-29 08:48
文章分类

全部博文(104)

文章存档

2015年(1)

2013年(13)

2012年(31)

2011年(59)

分类: C/C++

2011-12-15 10:51:41

结构体和联合体各自的基本用法不赘述,仅说一下他们匿名时访问的情况。如果是token不同,可以直接跨层访问。例子
  1. #include <iostream>
  2. using namespace std;

  3. struct zoo_obj{
  4.         string name;
  5.         union {
  6.                 unsigned int property;
  7.                 struct{ //plant
  8.                         unsigned int
  9.                                 hasLeaf:1,
  10.                                 hasFlower:1,
  11.                                 hasTrunk:1,
  12.                                 hasRattan:1;
  13.                 };
  14.                 struct { //animal
  15.                         unsigned int
  16.                                 isBackbone:1,
  17.                                 isOvipara:1,
  18.                                 hasLags:1,
  19.                                 hasWing:1;
  20.                 };
  21.         };

  22. };

  23. int main(void)
  24. {
  25.         zoo_obj peony = {"peony",0};
  26.         zoo_obj dog = {name:"dog",0};
  27.         dog.hasLags = true;
  28.         /* zoo_obj dog = {name:"dog",property:0};
  29.          * sorry, unimplemented: non-trivial designated initializers
  30.          * not supported
  31.          */

  32.         cout << peony.name << " hasLeaf " << peony.hasLeaf << endl;
  33.         cout << dog.name << " hasLags " << dog.hasLags << endl;

  34. }
输出
  1. peony hasLeaf 0
  2. dog hasLags 1
不支持像列子中name样的指定访问。如果指定了会报错
  1. sorry, unimplemented: non-trivial designated initializers not supported





阅读(2517) | 评论(2) | 转发(1) |
给主人留下些什么吧!~~

wwwkljoel2011-12-16 10:18:55

桔色花朵: 为什么匿名呢?.....
如果不匿名,那么访问的时候要层层指定,敲键麻烦,你可以试试如果起了名字,访问时试试,很讨厌的,当然了这时可以进行#defile 减少击键

桔色花朵2011-12-15 23:31:59

为什么匿名呢?