如果我有以下内容:
struct LineChartScene::LineChartSceneImpl
{
enum ContextMenuAction {ShowLabels, ShowPoints, SaveAsImage};
};
如何在ShowLabels
结构之外访问ShowPoints
,LineChartScene::LineChartSceneImpl
等?我认为LineChartScene::LineChartSceneImpl::ContextMenuAction::ShowLabels
会起作用,但事实并非如此。我正在使用C ++,Qt Creator 2.2.1。
答案 0 :(得分:9)
struct LineChartScene::LineChartSceneImpl
{
enum ContextMenuAction {ShowLabels, ShowPoints, SaveAsImage};
};
将其用作
LineChartScene::LineChartSceneImpl::ShowLabels
对于您的信息,C++11 also has strong typed enums具有您期望的名称空间语义:
enum class Enum2 : unsigned int {Val1, Val2};
枚举的范围也定义为枚举名称的范围。使用枚举器名称需要明确确定范围。
Val1
未定义,但Enum2::Val1
已定义。此外,C ++ 11将允许旧式枚举提供显式作用域以及基础类型的定义:
enum Enum3 : unsigned long {Val1 = 1, Val2};
枚举器名称在枚举的范围(
Enum3::Val1
)中定义,但为了向后兼容,枚举器名称也放在封闭范围内。
答案 1 :(得分:4)
使用:
LineChartScene::LineChartSceneImpl::ShowLabels
请注意,该行中没有ContextMenuAction
。这是因为枚举标签不是在枚举类型中的范围,而是它们在定义枚举的封闭范围内,并且在这种情况下,封闭范围是类类型。我知道它非常 unintuitive ,但这就是它的设计方式。
答案 2 :(得分:1)
您不需要枚举的名称,但是您走在正确的轨道上:
LineChartScene::LineChartSceneImpl::ShowLabels
答案 3 :(得分:1)
只需LineChartScene::LineChartSceneImpl::ShowLabels