-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenum_class.cpp
59 lines (50 loc) · 1.94 KB
/
enum_class.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
52
53
54
55
56
57
58
59
#include <iostream>
#include <string>
// Traditional enum for days of the week
enum DayOfWeek { MON, TUE, WED, THU, FRI, SAT, SUN };
// Strongly-typed enum for months
enum class Month {
JAN = 1, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC
};
// Function using traditional enum
void scheduleEvent(DayOfWeek day) {
// Problem: Accidental use of unrelated constant
if (day == 3) { // This compiles, but it's logically incorrect
std::cout << "Scheduling for Wednesday" << std::endl;
}
}
// Function using enum class
std::string getMonthName(Month month) {
switch (month) {
case Month::JAN: return "January";
case Month::FEB: return "February";
// ... other months ...
case Month::DEC: return "December";
default: return "Invalid month";
}
}
int main() {
// Traditional enum issues
DayOfWeek day = FRI;
int dayNumber = day; // Implicit conversion allowed
// Enum class benefits
Month month = Month::MAY;
// int monthNumber = month; // Error: no implicit conversion
int monthNumber = static_cast<int>(month); // Explicit conversion required
// Avoiding accidental comparisons
// if (month == FRI) {} // Error: can't compare Month with DayOfWeek
if (month == Month::MAY) {
std::cout << "It's May!" << std::endl;
}
// Using the enum class in a function
std::cout << "Month name: " << getMonthName(month) << std::endl;
return 0;
}
/*
Type safety: Month enum class prevents implicit conversions and accidental comparisons with unrelated types.
Scoped access: Values must be accessed as Month::JAN, Month::FEB, etc., improving code clarity.
Explicit conversions: Converting enum class to int requires a cast, preventing unintended conversions.
Stronger typing in function parameters: getMonthName function can only accept Month enum class values.
Comparison safety: Can't accidentally compare Month with DayOfWeek or arbitrary integers.
*/