Donotalo
Donotalo

Reputation: 13025

Accessing enum values defined in a struct

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 LineChartScene::LineChartSceneImpl::ContextMenuAction::ShowLabels would work but it doesn't. I'm using C++, Qt Creator 2.2.1.

Upvotes: 7

Views: 23713

Answers (4)

sehe
sehe

Reputation: 393084

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

use it as

LineChartScene::LineChartSceneImpl::ShowLabels

For your info, C++11 also has strong typed enums with precisely the namespace semantics you expected:

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

The scoping of the enumeration is also defined as the enumeration name's scope. Using the enumerator names requires explicitly scoping. Val1 is undefined, but Enum2::Val1 is defined.

Additionally, C++11 will allow old-style enumerations to provide explicit scoping as well as the definition of the underlying type:

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

The enumerator names are defined in the enumeration's scope (Enum3::Val1), but for backwards compatibility, enumerator names are also placed in the enclosing scope.

Upvotes: 10

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361472

Use :

LineChartScene::LineChartSceneImpl::ShowLabels

Notice that there is noContextMenuAction in the line. It is because the enum labels are not scoped within the enum type, rather they are scoped within the enclosing scope in which the enum is defined, and in this case the enclosing scope is the class type. I know it is very unintuitive, but that is how it is designed.

Upvotes: 5

zennehoy
zennehoy

Reputation: 6846

Just LineChartScene::LineChartSceneImpl::ShowLabels

Upvotes: 1

trojanfoe
trojanfoe

Reputation: 122391

You don't need the name of the enum, but you are on the right track:

LineChartScene::LineChartSceneImpl::ShowLabels

Upvotes: 1

Related Questions