枚举的使用 - 全局和局部
Usage of enum - global and local
我只是尝试使用与本地定义相同名称的全局定义枚举。
除起点外,我已使用相同的值进行了初始化。
enum Day {Sunday = 1 ,Monday,Tuesday,Wednesday,Thursday,Friday /*= 15 */,Saturday};
void enumUse() {
enum Day {Sunday = 2 ,Monday,Tuesday,Wednesday,Thursday,Friday /*= 15 */,Saturday};
Day today = Tuesday;
cout<<Sunday<<endl;
cout<<Monday<<endl;
cout<<Tuesday<<endl;
cout<<Wednesday<<endl;
cout<<Thursday<<endl;
cout<<Friday<<endl;
cout<<Saturday<<endl;
cout<<"Today: "<<today;
cout<<endl;
}
此代码片段给出了本地枚举的输出。
如果注释掉,会给出全局相关的输出。
如何在不注释本地枚举的情况下使用全局枚举。
可以使用作用域解析运算符,因为全局Day
是在全局作用域
中定义的
//'Day' from global scope
::Day today = ::Tuesday; //'Tuesday' also needs it because it would take the local one,
//which can't be assigned to the global 'Day' (they're different
//enums after all)
我只是尝试使用与本地定义相同名称的全局定义枚举。
除起点外,我已使用相同的值进行了初始化。
enum Day {Sunday = 1 ,Monday,Tuesday,Wednesday,Thursday,Friday /*= 15 */,Saturday};
void enumUse() {
enum Day {Sunday = 2 ,Monday,Tuesday,Wednesday,Thursday,Friday /*= 15 */,Saturday};
Day today = Tuesday;
cout<<Sunday<<endl;
cout<<Monday<<endl;
cout<<Tuesday<<endl;
cout<<Wednesday<<endl;
cout<<Thursday<<endl;
cout<<Friday<<endl;
cout<<Saturday<<endl;
cout<<"Today: "<<today;
cout<<endl;
}
此代码片段给出了本地枚举的输出。
如果注释掉,会给出全局相关的输出。
如何在不注释本地枚举的情况下使用全局枚举。
可以使用作用域解析运算符,因为全局Day
是在全局作用域
//'Day' from global scope
::Day today = ::Tuesday; //'Tuesday' also needs it because it would take the local one,
//which can't be assigned to the global 'Day' (they're different
//enums after all)