开发者

doubt resolving namespaces in enums?

from the below question i sort of get how enums and namespace scoping works

Scope resolution operator on enums a compiler-specific extension?

However with regard to test code below i'm confused as to why in the below code snippet:

1) i can refer to return type in function signature as test_enum::foo_enum

2) however "using namespace test_enum::foo_enum" is not allowed

namespac开发者_JAVA百科e  test_enum { 

   enum foo_enum { 

      INVALID, 
       VALID
    };
} 

// Case 1) this is allowed 
test_enum::foo_enum getvalue() {

     return test_enum::INVALID;

}

//Case 2) is not allowed 

using namespace test_enum::foo_enum; 

is there a particular reason for not allowing case 2 ?

Also are "enums" more of C style construct and better to avoid in C++ code ?


The reason using namespace test_enum::foo_enum; is not allowed is because foo_enum is not a namespace, it is an enum. What works is using test_enum::foo_enum;

I believe what you are trying to do is something like this:

namespace foo_enum {
    enum foo_enum_t {
        INVALID,
        VALID,
    };
}

using foo_enum::foo_enum_t;

This allows you to throw around foo_enum_t freely, but you still have to type out foo_enum::INVALID or foo_enum::VALID

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜