Access to enumeration values ​​defined in the structure

If I have the following:

struct LineChartScene::LineChartSceneImpl { enum ContextMenuAction {ShowLabels, ShowPoints, SaveAsImage}; }; 

How can I access ShowLabels , ShowPoints , etc. outside LineChartScene::LineChartSceneImpl struct? I thought that LineChartScene::LineChartSceneImpl::ContextMenuAction::ShowLabels will work, but it is not. I am using C ++, Qt Creator 2.2.1.

+6
source share
4 answers
 struct LineChartScene::LineChartSceneImpl { enum ContextMenuAction {ShowLabels, ShowPoints, SaveAsImage}; }; 

use it like

 LineChartScene::LineChartSceneImpl::ShowLabels 

For your information, C ++ 11 also has strongly typed enumerations with the exact specified namespace semantics that you expected:

 enum class Enum2 : unsigned int {Val1, Val2}; 

The enumeration scope is also defined as the enumeration name scope. The use of enumerator names requires an explicit review. Val1 is undefined, but Enum2::Val1 .

In addition, C ++ 11 allows old-style enumerations to provide an explicit definition of the scope, as well as a definition of the base type:

 enum Enum3 : unsigned long {Val1 = 1, Val2}; 

Enumeration names are defined in the enumeration Enum3::Val1 ( Enum3::Val1 ), but for backward compatibility, enumeration names are also placed in the encompassing scope.

+9
source

Using:

 LineChartScene::LineChartSceneImpl::ShowLabels 

Note that there is no ContextMenuAction in the line. This is because enumeration labels are not limited to the type of enumeration, but rather they are covered by the scope of the application in which the enumeration is defined, in which case the encompassing area is a class type. I know this is very unintuitive, but that is how it is designed.

+4
source

You don't need a rename name, but you're on the right track:

 LineChartScene::LineChartSceneImpl::ShowLabels 
+1
source

Just LineChartScene::LineChartSceneImpl::ShowLabels

+1
source

Source: https://habr.com/ru/post/902009/


All Articles