Phần 1
1. Goal
Before learning enum class, you should understand old-style enum.
After this lesson, you should understand:
1. What an enum is.
2. Why enum is better than raw integers.
3. The minimum syntax.
4. Common problems with old-style enum.
5. Why C++11 introduced enum class.Phần 2
2. What Problem Does enum Solve?
An enum represents a fixed set of named values.
Instead of writing unclear integers:
int side = 0; // What does 0 mean?
int status = 2; // What does 2 mean?Use named values:
enum Side {
Buy,
Sell
};Main idea:
Use enum when a variable can only have a small fixed set of valid values.Examples:
Side: Buy / Sell
OrderStatus: New / Filled / Cancelled
OrderType: Market / Limit
ConnectionState: Connected / DisconnectedPhần 3
3. Minimum Syntax
Define an enum:
enum Side {
Buy,
Sell
};Create a variable:
Side side = Buy;Compare enum values:
if (side == Buy) {
// buy order
}Use enum in a switch:
switch (side) {
case Buy:
break;
case Sell:
break;
}Phần 4
4. What Values Do Enum Members Have?
By default, enum values start from 0.
enum Side {
Buy, // 0
Sell // 1
};You can also assign explicit values:
enum OrderStatus {
New = 1,
Filled = 2,
Cancelled = 3,
Rejected = 4
};Phần 5
5. Common Mistakes
5.1. Enum names leak into the surrounding scope
enum Side {
Buy,
Sell
};
enum Action {
Buy,
Cancel
};This causes a name conflict because both enums define Buy in the same scope.
5.2. Old-style enum can convert to int
Side side = Buy;
int x = side;This compiles because old-style enum can implicitly convert to integer.
This can be dangerous because it weakens type safety.
5.3. Raw integers can still be confusing
Avoid:
int side = 0;Prefer:
Side side = Buy;Named values are much easier to understand than magic numbers.
Phần 6
6. Why This Matters Before enum class
Old-style enum is useful, but it has weaknesses:
1. Names are not scoped.
2. Values can convert to int.
3. Different enums can conflict with each other.
4. Type safety is weaker.C++11 introduced enum class to fix these problems.
Phần 7
7. Notes for Trading Code
Trading systems often use fixed sets of domain values:
Side
OrderType
OrderStatus
TimeInForce
Exchange
MessageTypeUsing enums makes code clearer than raw integers.
Example idea:
Buy is clearer than 0.
Sell is clearer than 1.
Filled is clearer than 2.But old-style enum has type-safety problems, so modern C++ usually prefers enum class.
Phần 8
8. End-of-Day Checklist
You should be able to answer:
1. What is an enum?
2. Why is enum better than raw integers?
3. What value does the first enum member have by default?
4. Why can old-style enum cause name conflicts?
5. Why did C++11 introduce enum class?Phần 9
9. Conclusion
Old-style enum is used to represent a fixed set of named values.
The key idea:
enum improves readability compared to raw integers,
but old-style enum has scope and type-safety problems.This is why C++11 introduced enum class.
:::
Mã mẫu hoàn chỉnh
cpp11/5_enum/main.cpp
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
enum Side {
Buy,
Sell
};
enum OrderType {
Market,
Limit
};
enum OrderStatus {
New,
Filled,
Cancelled,
Rejected
};
struct Order {
int id;
std::string symbol;
Side side;
OrderType type;
OrderStatus status;
double price;
int quantity;
};
std::string sideToString(Side side) {
switch (side) {
case Buy:
return "Buy";
case Sell:
return "Sell";
}
return "Unknown";
}
std::string typeToString(OrderType type) {
switch (type) {
case Market:
return "Market";
case Limit:
return "Limit";
}
return "Unknown";
}
std::string statusToString(OrderStatus status) {
switch (status) {
case New:
return "New";
case Filled:
return "Filled";
case Cancelled:
return "Cancelled";
case Rejected:
return "Rejected";
}
return "Unknown";
}
bool isActive(OrderStatus status) {
return status == New;
}
void cancelOrder(Order& order) {
if (isActive(order.status)) {
order.status = Cancelled;
}
}
void printOrder(const Order& order) {
std::cout << "Order ID: " << order.id << '\n';
std::cout << "Symbol: " << order.symbol << '\n';
std::cout << "Side: " << sideToString(order.side) << '\n';
std::cout << "Type: " << typeToString(order.type) << '\n';
std::cout << "Status: " << statusToString(order.status) << '\n';
std::cout << "Price: " << std::fixed << std::setprecision(2)
<< order.price << '\n';
std::cout << "Quantity: " << order.quantity << '\n';
}
int main() {
std::vector<Order> orders = {
{101, "AAPL", Buy, Limit, New, 191.25, 100},
{102, "MSFT", Sell, Market, Filled, 0.00, 50},
{103, "NVDA", Buy, Limit, New, 875.10, 10}
};
std::cout << "Before cancel:\n";
printOrder(orders[0]);
cancelOrder(orders[0]);
std::cout << "\nAfter cancel:\n";
printOrder(orders[0]);
std::cout << "\nEnum integer values:\n";
std::cout << "Buy = " << Buy << '\n';
std::cout << "Sell = " << Sell << '\n';
std::cout << "New = " << New << '\n';
std::cout << "Filled = " << Filled << '\n';
std::cout << "Cancelled = " << Cancelled << '\n';
std::cout << "Rejected = " << Rejected << '\n';
return 0;
}Tự kiểm tra · không chấm điểm
Bạn đã hiểu những điểm nào?
Đã tự kiểm tra 0/5 mục