zoukankan      html  css  js  c++  java
  • 为什么使用#define 而不是用enum定义常量

    typedef enum {
        IOTAG_PORT__A = (0),
        IOTAG_PORT__B,
        IOTAG_PORT__C,
        IOTAG_PORT__F,
        IOTAG_PORT__ITEMS
    } iotagPort_e;
    
    #if (IOTAG_PORT__F)    // IOTAG_PORT__F未define,默认为0
    #    error "can you see me?"
    #endif

    比如上面的例子,错误信息是看不到的,因为IOTAG_PORT__F并未预定义,换句话说enum里面的常量你不能用预编译命令作为条件判断,这就很致命了。

    反过来说,enum中的常量你不用于预编译条件判断中,那就自然是枚举变量更好些。

    此外,enum是可以指定基础变量类型的:

    typedef enum :uint8_t{ // 指定enum的基础类型是uint8_t
        IOTAG_PORT__A = (0),
        IOTAG_PORT__B,
        IOTAG_PORT__C,
        IOTAG_PORT__F,
        IOTAG_PORT__ITEMS
    } iotagPort_e;
    
    
    
    
  • 相关阅读:
    css布局模型
    HTML元素分类
    《水经注》卷三十五
    《水经注》卷二十八
    沧浪之水
    网页布局基础
    IndexError: tuple index out of range
    树回归-CART
    树回归-CART
    支持向量机SVM
  • 原文地址:https://www.cnblogs.com/qiyuexin/p/15252709.html
Copyright © 2011-2022 走看看