zoukankan      html  css  js  c++  java
  • c++官方文档-枚举-联合体-结构体-typedef-using

    #include<iostream>
    #include <new>
    #include<stdio.h>
    using namespace std;
    /**
     * url:http://www.cplusplus.com/doc/tutorial/other_data_types/
     */
    /**
     * in which all its member elements occupy the same physical space in memory.
     * The size of this type is the one of the largest member element
     */
    union mytypes_t
    {
        char c;
        int i;
        float f;
    } mytypes;
    
    /**
     * 枚举类,注意不是枚举类型,语法如下
     */
    //They are declared with enum class (or enum struct) instead of just enum:
    enum class Colors
        : long
        {
            black, blue, green
    };
    enum Color2
    {
        black2, blue2, green2, white
    };
    
    enum class EyeColor
        : char
        {    blue, green, brown
    };
    
    //大神解说 https://zhuanlan.zhihu.com/p/21722362
    //enum Color
    //{
    //    //编译出错,重新定义white
    //    black, white, red
    //equals this
    //#define black 0
    //#define white 1
    //#define red 2
    //};
    //that is ok
    enum  class Color
    {
        black, white, red
    };
    
    //        auto white = false; // error
    
    template<class T> void f(T t)
    {
        using tt = T;
        tt t2;
        t2 = t;
        typedef T ttt;
        ttt t3 = t;
    }
    int main()
    {
        /**
         * 类型别名
         * 继承自c
         * typedef existing_type new_type_name;
         * c++
         * using new_type_name=existing_type
         */
        typedef char C_1;
        C_1 c = 'A';
        using C_2 = char;
        C_2 cc = 'B';
        f(10);
        Colors mycolor;
        mycolor = Colors::black;
        if(mycolor == Colors::black)
        {
            mycolor = Colors::blue;
        }
        //只有一个字节长度噢
        cout << sizeof(EyeColor) << endl;
        cout << sizeof(Colors) << endl;
        cout << sizeof(Color2) << endl;
        return 0;
    }
  • 相关阅读:
    变量
    总结 对象
    学生管理系统
    [Altera] Device Part Number Format
    [Matlab] sum
    [Matlab] Galois Field
    [C] static和extern的作用
    [Python] list
    [Python] raw_input
    [软件] UnicornViewer
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/8160733.html
Copyright © 2011-2022 走看看