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;
Jmoney38
  • 3,096
  • 2
  • 25
  • 26

3 Answers3

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.

Community
  • 1
  • 1
Chris Frederick
  • 5,482
  • 3
  • 36
  • 44
  • I think you should replace the dot (`.`) with `::` aftert `MyClass` – Kiril Kirov Jun 09 '11 at 19:53
  • @Kiril: Thanks for pointing that out. I guess that's what I get for copying and pasting. :) – Chris Frederick Jun 09 '11 at 19:54
  • 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. – Jmoney38 Jun 09 '11 at 20:04
  • 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... – Jmoney38 Jun 09 '11 at 20:10
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

Community
  • 1
  • 1
ptpaterson
  • 9,131
  • 4
  • 26
  • 40
  • I jumped ahead of myself and misunderstood what you were going after. – ptpaterson Jun 09 '11 at 20:22
  • 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 :-) – Jmoney38 Jun 10 '11 at 11:48
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.

Puppy
  • 144,682
  • 38
  • 256
  • 465