-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum.cpp
More file actions
72 lines (53 loc) · 1.14 KB
/
enum.cpp
File metadata and controls
72 lines (53 loc) · 1.14 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
// unscoped enum
enum Color: int {
red,
blue,
green
};
void foo(Color color) {
switch (color) {
case red:
std::cout << "red" << std::endl;
break;
case blue:
std::cout << "blue" << std::endl;
break;
case green:
std::cout << "green" << std::endl;
break;
}
}
// scoped enum
enum struct Color2: int {
red,
blue,
green
};
void bar(Color2 color) {
switch (color) {
case Color2::red:
std::cout << "red" << std::endl;
break;
case Color2::blue:
std::cout << "blue" << std::endl;
break;
case Color2::green:
std::cout << "green" << std::endl;
break;
}
}
// creating own type
enum byte : unsigned char{};
enum INT : int {};
// char uchar int uint long ulong
enum uchar : unsigned char {};
enum class fruit { orange, apple };
int main() {
foo(red);
bar(Color2::red);
uchar c = static_cast<uchar>(100);
byte b = static_cast<byte>(10);
INT i = static_cast<INT>(100);
return 0;
}