C/C++ enum 用法與範例

本篇 ShengYu 介紹 C/C++ enum 列舉用法與範例,

以下 C/C++ enum 列舉的用法介紹將分為這幾部份,

  • C/C++ enum 基本用法
  • C/C++ enum 指定值
  • C/C++ typedef enum 取別名
  • C/C++ enum class 限制 scope C++11
  • C/C++ enum 繼承

那我們開始吧!

C/C++ enum 基本用法

C/C++ enum 列舉預設從 0 開始,後續的列舉項目如果後面不指定值的話預設會累加 1,基本範例如下,

cpp-enum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// g++ cpp-enum.cpp -o a.out
#include <stdio.h>

enum fruit {
apple,
banana,
lemon,
mango,
orange
};

int main() {
printf("%d\n", apple);
printf("%d\n", banana);
printf("%d\n", lemon);
printf("%d\n", ::mango);
printf("%d\n", ::orange);

printf("===\n");
fruit f = apple;
printf("%d\n", f);

return 0;
}

輸出如下,可以看到 apple 的值為 0,因為 enum 列舉從 0 開始,
banana 的值為 1,因為 enum 列舉會自動加 1,後續依此類推,
列舉項目是全域的,所以加上 :: 也是一樣的意思,如上例中的 ::mango
另外 enum 列舉可以用來宣告變數,然後存放這個列舉項目,如上例中的 f 變數,

1
2
3
4
5
6
7
0
1
2
3
4
===
0

C/C++ enum 指定值

C/C++ enum 列舉如果要指定值的話,可以這樣寫,

cpp-enum2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// g++ cpp-enum2.cpp -o a.out
#include <stdio.h>

enum fruit {
apple = 1,
banana = 2,
lemon,
mango,
orange
};

int main() {
printf("%d\n", apple);
printf("%d\n", banana);
printf("%d\n", lemon);
printf("%d\n", mango);
printf("%d\n", orange);

printf("===\n");
fruit f = lemon;
printf("%d\n", f);

return 0;
}

輸出如下,指定 apple 為 1,指定 banana 為 2,所以 lemon 會繼續累加變成 3,後續依此類推,

1
2
3
4
5
6
7
1
2
3
4
5
===
3

那如果 banana 與 lemon 同樣指定為 2 後續會是怎樣呢?

1
2
3
4
5
6
7
enum fruit {
apple = 1,
banana = 2,
lemon = 2,
mango,
orange
};

結果會是這樣,mango 還是會繼續累加變成 3,後續依此類推,

1
2
3
4
5
1
2
2
3
4

C/C++ typedef enum 取別名

C/C++ 用 typedef 可以將某 enum 取一個新別名,以下示範用 typedef 將 fruit 這個 enum 取一個 FRUIT 新別名,之後宣告時就可以使用新的 FRUIT 別名,就可以省去加上 enum,藉此達到簡化宣告語法,

cpp-enum3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// g++ cpp-enum3.cpp -o a.out
#include <stdio.h>

typedef enum fruit {
apple,
banana,
lemon,
mango,
orange
} FRUIT;

int main() {
fruit f1 = apple;
FRUIT f2 = apple;
FRUIT f3 = banana;
printf("%d\n", apple);
printf("%d\n", f1);
printf("%d\n", f2);
printf("%d\n", f3);

return 0;
}

輸出如下,

1
2
3
4
0
0
0
1

另外還有另外一種寫法,可以把 union 的定義跟 typedef 分開寫,typedef 最後面記得要加上分號,

1
2
3
4
5
6
7
8
9
enum fruit {
apple,
banana,
lemon,
mango,
orange
};

typedef enum fruit FRUIT;

C/C++ enum class 限制 scope C++11

範圍列舉 scoped enumerations C++11 才支援的,一般的 enum 是全域範圍,也就是不能有另外一個 enum 裡面也有相同名稱的列舉項目,
以下面例子 fruit2 為例,enum 裡面也跟 fruit 同樣有同名稱的 apple, banana, lemon,就會出現編譯錯誤,
為了解決這個問題,C++11 就有了 scoped enumerations 可以限制這個列舉的範圍,

cpp-enum4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// g++ cpp-enum4.cpp -o a.out -std=c++11
#include <stdio.h>
#include <stdint.h>

enum fruit {
apple,
banana,
lemon
};

//enum fruit2 { // Compiler Error, redefinition
// apple,
// banana,
// lemon
//};

enum class fruit3 {
apple,
banana,
lemon
};

enum class fruit4 {
apple,
banana,
lemon
};

int main() {
printf("%d\n", apple);
printf("%d\n", ::banana);
printf("%d\n", ::lemon);

printf("===\n");
printf("%d\n", fruit3::apple);
printf("%d\n", fruit3::banana);
printf("%d\n", fruit3::lemon);

printf("===\n");
printf("%d\n", fruit4::apple);
printf("%d\n", fruit4::banana);
printf("%d\n", fruit4::lemon);

printf("===\n");
fruit4 f = fruit4::apple;
//fruit4 f2 = apple; // Compiler Error
printf("%d\n", f);

return 0;
}

輸出如下,

1
2
3
4
5
6
7
8
9
10
11
12
13
0
1
2
===
0
1
2
===
0
1
2
===
0

C/C++ enum 繼承

C/C++ enum 繼承後會影響該 enum 列舉的大小,enum 列舉繼承指定類型的大小主要是可以節省記憶體,如下範例所示,

cpp-enum5.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// g++ cpp-enum5.cpp -o a.out -std=c++11
#include <stdio.h>
#include <stdint.h>

enum color {
red,
yellow,
blue
};

enum fruit2 : int8_t {
apple,
banana,
lemon
};

//enum fruit3 : int8_t { // Compiler Error, redefinition
// apple,
// banana,
// lemon
//};

enum class fruit4 : int8_t {
apple,
banana,
lemon
};

enum class fruit5 : int32_t {
apple,
banana,
lemon
};

int main() {
printf("%d\n", sizeof(color::red));
printf("%d\n", sizeof(fruit2::apple));
printf("%d\n", sizeof(fruit4::apple));
printf("%d\n", sizeof(fruit5::apple));

color c = red;
printf("%d\n", sizeof(color::red));
fruit2 f2;
printf("%d\n", sizeof(f2));
fruit4 f4;
printf("%d\n", sizeof(f4));
fruit5 f5;
printf("%d\n", sizeof(f5));

return 0;
}

輸出如下,

1
2
3
4
5
6
7
8
4
1
1
4
4
1
1
4

以上就是 C/C++ enum 的用法與範例介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!

其它相關文章推薦
如果你想學習 C/C++ 相關技術,可以參考看看下面的文章,
C/C++ 新手入門教學懶人包
C/C++ union 用法與範例
C/C++ struct 用法與範例