gpt4 book ai didi

c++ - 类中的枚举,具有显式作用域,C++11 之前的版本?

转载 作者:行者123 更新时间:2023-11-30 02:00:22 28 4
gpt4 key购买 nike

我使用的 VS2010 没有 C++11 的强类型枚举。我可以没有强类型,但无论如何,我希望将枚举排除在我的类的命名空间之外。

class Example{
enum Color{
red,
green,
blue
};

int Rainbows{
Color x = red; // this should be impossible
Color y = Color::green; // this is the only way at the enumerations
}
};

我的问题是,在 C++11 之前,实现这一目标的最佳方法是什么?

最佳答案

namespace ExampleColor {
enum Color {
red,
green,
blue
};
}

class Example {
int Rainbows{ExampleColor::Color x = ExampleColor::red};
};

关于c++ - 类中的枚举,具有显式作用域,C++11 之前的版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15216857/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com