-8

In c++11 what is the c++ "way" (as opposed to the that used in c) of referencing enums defined in a class? E.g

foo.BAR vs Foo::States::BAR

I've always used the latter but in some places, the former may make as much sense.

6
  • 2
    What does "preferred way" mean? Jul 19, 2014 at 2:48
  • I understand c++ is backwards compatible with c, so was wondering if there's a specific c++ way
    – anon
    Jul 19, 2014 at 2:53
  • 3
    Usually when I'm thinking about enums I'm thinking about types. I can't think of a situation where I want an enum value to look similar to a member variable so because of this I like the 2nd syntax.
    – shuttle87
    Jul 19, 2014 at 2:54
  • Assuming States is the enum, the second way is only valid since C++11 gor a compiler that offered the syntax as an extension), but Foo::BAR has been valid for decades. Jul 19, 2014 at 3:31
  • Why the down votes? This is a valid question and nothing to do with coding styles
    – anon
    Jul 19, 2014 at 3:46

2 Answers 2

7

In all versions of C++, the second version (Foo::States::BAR) using scope syntax is the more conventional and will be less surprising for future maintainers of your code. Since the value is a constant, there is no need for an instance of the class, so this is similar to how static methods are most often called with scope syntax rather than instance syntax.

The one motivation for calling static methods on an instance might be to make it more similar (compatible) with a non-static method call, but that motivation is largely irrelevant for your enum example, not least of all because a possible future change to make your enum into a variable would make it look "wrong," i.e. BAR in all caps is understood by most programmers to be a constant value, not a variable, and it has been this way since perhaps the 1970's.

2

The second way :: is definitely preferable for a few reasons.

Conceptually enums are really like types, they specify the values that a certain variable can take. This doesn't depend on the instance of that variable though. If you put enum values in a class your enum values are not dependent on an instance of the class. In that way they act very much like static members. So I would choose the syntax that is consistent for static members for this reason.

As per the c++ standard you can use foo.BAR but the usual connotations of this syntax make the code less clear. It looks like you are trying to access a member of an instance when you really are not. This syntax makes me think about member variables when really I should be thinking about enumeration values.

You can also use foo->BAR if the enumeration is in scope, but I would argue that this makes the intent even less clear. This syntax makes me think about pointers when really I should be thinking about enumeration values.

Additionally if you are working with other programmers they will be expecting to see the :: way of accessing the enums. Using the other ways will be more confusing to other people reading your code.