C++的enum可以限制成员的类型
//error C2440: “=”: 无法从“int”转换为“color”
1 #include <iostream> 2 using namespace std; 3 4 enum color :char { red = ‘A‘, yellow, green, white };//限制为char类型 5 6 void main() 7 { 8 color mycolor(color::red); 9 10 //mycolor = 1;//error C2440: “=”: 无法从“int”转换为“color” 11 12 //mycolor = ‘A‘;//error C2440: “=”: 无法从“char”转换为“color” 13 14 mycolor = color::white; 15 16 std::cout << red << " " << yellow << std::endl;//65 66 17 18 system("pause"); 19 }
时间: 2024-11-13 21:37:17