10

I have a good understanding of how the C++ 'using' declaration and directive work. However, I'm stumped on this... Maybe it's not possible? I want to avoid having to quality my enum variables:

namespace Foo { 
   class MyClass {
      public: 
         enum MyEnum { X, Y, Z };
   }
}

And now, from outside that namespace, I would like to be able to do things like:

using Foo::MyClass.MyEnum;
MyEnum letter = MyEnum::x;

But apparently that's not the way to do it? I'm betting this is possible, but my notation is wrong... I also tried using Foo::MyClass::MyEnum, but then the compiler thinks Foo::MyClass is a namespace.

Added: As you can see, it becomes annoying having to fully declare everything...

Foo::MyClass::MyEnum value = Foo::MyClass::X;
3
  • Where you try to put the using ? Inside a class, or? Commented Jun 9, 2011 at 19:52
  • possible duplicate of using declaration with enum? Commented Jun 9, 2011 at 19:58
  • @Kiril, Inside of a method that is in a different class that is NOT in the Foo namespace. Commented Jun 9, 2011 at 20:05

3 Answers 3

6

This doesn't answer your question directly, but if you want to economize keystrokes you could try using a typedef instead.

typedef Foo::MyClass::MyEnum MyClassEnum;

By the way, it looks like your question has been asked on Stack Overflow before. From the answer to that question:

A class does not define a namespace, therefore "using" isn't applicable here.

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

4 Comments

I think you should replace the dot (.) with :: aftert MyClass
@Kiril: Thanks for pointing that out. I guess that's what I get for copying and pasting. :)
I read that article before posting, but it doesn't seem to address my concern whereby the class is within a namespace, AND an outside-of-the-namespace client wants to include that specific enum. I understand that the enumerator will not be automatically brought into the local scope, that's fine.
And yes, I could typedef it I suppose. If it's just not possible to do for some reason, then I guess that would be the best solution...
1

C++03 does not support fully qualifying enum types, but it's an MSVC extension. C++0x will make this Standard, and I believe that you can using an enum in C++0x. However, in C++03, I don't believe that your problem can be solved.

Comments

1

I got the following to compile, after messing around a lot. I think that it will be the closest you can get without typedef'ing.

namespace Foo {
    class MyClass {
        public:
            enum MyEnum { X, Y, Z };
    };
};

namespace Foo2 {
    using Foo::MyClass; 

    class AnotherClass {
        public:

            AnotherClass(){
                MyClass::MyEnum value = MyClass::X; 
            }
    };
};

int main(){

    return 1;
}

You could also use MyClass as a base class, but I am doubting that's something you want to do.

namespace Foo2 {
    using namespace Foo;
    class AnotherClass : public MyClass{
        public:
            AnotherClass(){
                MyEnum value = X; 
            }
    };
};

This also has some good info. namespaces for enum types - best practices

2 Comments

I jumped ahead of myself and misunderstood what you were going after.
Thanks for the input - However, like you said, I would rather be able to use the MyEnum without the scope operator for the MyClass. Points for effort though :-)

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.