C++ 结构体和枚举

共同体

共同体(union) 是一种数据格式, 它能够存储不同的数据类型, 但只能同时存储其中的一种类型。也就是说, 结构可以同时存储int、long 和 double, 共同体只能存储int、long、double。

union one4all {
  int int_val;
  long long_val;
  double double_val;
}

没有细研究,暂时先占个位置

枚举

int main(int argc, char const *argv[])
{
    enum {zero, one, two, three};
    std::cout << three << '
';
    return 0;
}


答案:
========================

  root@corleone:/opt/code/testC++/右元/函数# g++ first.cpp -o main
  root@corleone:/opt/code/testC++/右元/函数# ./main
  3

 
#include <iostream>


int main(int argc, char const *argv[]) {

  using namespace std;
  enum bits{one=1};
  bits a;
  a = one;
  std::cout << "a:" << a << '
';


  return 0;
}

==================================

#include <iostream>


using namespace std;


int main(int argc, char const *argv []){


enum name { one = 123 };
int a = one;
printf("%d ", a);


return 0;


}

 
原文地址:https://www.cnblogs.com/renfanzi/p/7359101.html