c++11 枚举enum

C++11中enum的新特性:限制枚举值大小与枚举类
文章介绍了C++11对enum的两个新特性:一是可以限制枚举变量的值大小,例如通过`enumDEV_TYPE:unsignedchar`限制最大值;二是引入了枚举类(enumclass),这可以限制枚举的作用域,避免命名冲突。通过枚举类,可以更安全地使用枚举,比如在示例中防止了与全局变量red的冲突。

        看到 c++11 中 enum 的两个新用法,记录一下。

1,限制枚举变量值大小

#include <stdio.h>
#include <stdlib.h>

enum DEV_TYPE: unsigned char
{
    DEV_A,
    DEV_B,
    DEV_C,
    DEV_D = 255
};

int main()
{
    return 0;
}

类似于 class 中的继承的语法,这里的作用是限制了 enum 元素的最大值,如上 DEV_D 的最大值就是 255,如果赋值大于 255 会怎样呢?

#include <stdio.h>
#include <stdlib.h>

enum DEV_TYPE: unsigned char
{
    DEV_A,
    DEV_B,
    DEV_C,
    DEV_D = 256
};

int main()
{
    return 0;
}

编译失败: 

2,枚举类

#include <stdio.h>
#include <stdlib.h>

enum class COLOR
{
    white,
    red,
    black,
    gray,
    green
};

int main()
{
    return 0;
}

这样用有什么好处----限制了枚举的变量的作用域。如果没有这个作用域是怎样的呢?如:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    enum COLOR
    {
        white,
        red,
        black,
        gray,
        green
    };

    bool red = true;
    return 0;
}

编译错误,因为这个main作用域内已经有了一个枚举类型值 red。改用枚举类就不一样了:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    enum class COLOR 
    {
        white,
        red,
        black,
        gray,
        green
    };

    bool red = true;

    //使用域限定符
    COLOR color = COLOR::red;
    
    return 0;
}

有了枚举类限定了枚举类型的作用域,减少了命名空间的冲突。 

### C++ 中 `enum` 和 `enum class` 的区别 #### 1. 类型安全性 传统 `enum` 是弱类型的,其值默认可以隐式转换为整数类型。而 `enum class` 则是强类型的,不允许隐式转换为其他类型[^3]。 ```cpp // Traditional enum (weak type safety) enum Color { Red, Green, Blue }; void setColor(Color c) { // Function implementation } int main() { int value = Red; // Allowed due to implicit conversion. setColor(Blue); // Correct usage. setColor(Green + 1); // Implicitly converts integer back to Color. return 0; } ``` 对于 `enum class`,这种隐式转换被禁止: ```cpp // Enum class (strong type safety) enum class Direction { North, South, East, West }; void setDirection(Direction d) { // Function implementation } int main() { // int value = Direction::North; // Compilation error: no implicit conversion allowed. setDirection(Direction::South); return 0; } ``` #### 2. 命名冲突 传统 `enum` 的成员会注入到外部作用域中,容易引发命名冲突。相比之下,`enum class` 将枚举成员严格限制在其自身的范围内[^1]。 ```cpp // Example of naming conflict with traditional enum enum Status { Success, Failure }; enum Result { Success, Error }; // Causes a compilation error because 'Success' is already defined. // With enum class, there are no conflicts as the members belong only within their scope. enum class State { Success, Failure }; enum class Outcome { Success, Error }; // No issue here since scopes are separate. ``` #### 3. 定义方式 两者的定义语法略有不同。以下是具体的对比示例[^2]: ```cpp // Definition using traditional enum enum Animal { Cat, Dog, Bird }; // Definition using enum class enum class Pet { Fish, Hamster, Turtle }; ``` 注意,在访问 `enum class` 成员时需要显式指定所属的枚举类名称: ```cpp Pet myPet = Pet::Fish; // Required syntax for scoped enumerations. Animal pet = Bird; // Direct access works fine without specifying namespace or prefix. ``` #### 4. 使用场景 当项目规模较小且不需要过多考虑扩展性和维护成本时可以选择普通的 `enum`;然而如果追求更高的代码质量标准,则推荐采用 `enum class` 来增强程序的安全性并减少潜在错误风险。 --- ### 总结表 | 特性 | `enum` | `enum class` | |--------------------|----------------------------------|------------------------------------| | **类型安全** | 弱类型 | 强类型 | | **命名空间隔离** | 不支持 | 支持 | | **隐式转换至基础类型**| 允许 | 禁止 | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值