3

For enums within a class...

Incorrect :

class MyClass{
             public:
               enum kHTTPMethods {GET,PUT,POST}
};

void MyClass::Func(){
    kHTTPMethods method = kHTTPMethod.GET;
}

1) Am I right to say this does not work because . operator can only be use on objects (instances) of a class?

Correct:

void MyClass::Func(){
    kHTTPMethods method = GET; 
}

2) Am I right to say this is correct because all the elements of the class becomes globally scoped within the class?

2
  • This is basically correct but the choice of words isn't right. "Globally scoped within the class" makes no sense. You want to say "in scope within the class". Commented Jul 23, 2014 at 6:30
  • I have added this to what I have learned today. Commented Jul 23, 2014 at 16:04

2 Answers 2

7

Well, for your first question the issue is that the enumerations are put in the scope of class MyClass. Anyway, whether or not you have an object, . wouldn't let you refer to the enumerations, you need :: to refer to things in a specific scope. The following will compile, but the corrected MyClass:: scope isn't necessary or useful (i.e. you can just say method = GET because func is in the same scope as GET).

class MyClass
{
  public:
    enum kHTTPMethods {GET,PUT,POST};
    void func() {
        kHTTPMethods method = MyClass::GET;
    }
};

C++11 added an enum class that puts them in their own nested scope (such that you must prefix them with [...::MyClass:: ] kHTTPMethods:: everywhere), but you have to change your code to use that:

class MyClass
{
  public:
    enum class kHTTPMethods {GET,PUT,POST};    // note "class" after "enum"
    void func() {
        kHTTPMethods method = kHTTPMethods::GET;
    }
};

For your second question, yeah - that's about the size of it, though I'd phrase it as I have above.

Sign up to request clarification or add additional context in comments.

2 Comments

Great description and reflection of my understanding. Another question is before c++11, this cause namespace pollution, as in func() and GET share the same scope. Am I correct to state this?
@EthanLim yes, with enum X the enclosing class scope would get all the enumerations dumped into it, whereas with the C++11 enum class X you're polluting a new scope named X, and only the enum name X continues to pollute the containing (class) scope... that's quite handy when it makes sense to have several enums in the same scope with identical enumeration values (e.g. fairly often convenient for identifiers like Unimplemented, Cardinality, Default etc).
1

For your 1st question, YES. As far as I know, the . operator is only used on object to access member variables and member methods.

For your 2nd question. YES.

Everything within the brackets of MyClass::Func(){} has class scope, because of the MyClass:: preceding Func(). Meanwhile, GET has class scope too. Thus, you can refer to GET directly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.