zoukankan      html  css  js  c++  java
  • c++ anonymous union,struct -- 匿名联合体和机构体

    c++ anonymous union,struct -- 匿名联合体和机构体

    结构体和联合体各自的基本用法不赘述,仅说一下他们匿名时访问的情况。如果是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
     
     
  • 相关阅读:
    【7.19 graphshortestpath graphallshortestpaths函数】matlab 求最短路径函数总结
    【7.18 灾情巡视路线代码】
    【7.18总结】KM算法
    【7.17总结】 匈牙利算法(二分图最大匹配)
    动态规划 多段图最短路 有向图
    matlab 单元最短路 Dijkstra算法 无向图
    hdu 3536【并查集】
    博弈随笔
    AtCoder Regular Contest 094 D Worst Case【思维题】
    CODE FESTIVAL 2017 qual B C 3 Steps(补题)
  • 原文地址:https://www.cnblogs.com/timssd/p/4781109.html
Copyright © 2011-2022 走看看