views:

49

answers:

3

I have a class that includes an enum:

class appearance{
  // ... stuff ...
  enum color {BLUE, RED, GREEN};
};

I would like to attach part of the namespace (with using) so that I can refer to the value of the BLUE simply as BLUE, rather than appearance::BLUE. At the same time, I would like to keep the enum within the class{}, since I think that is most natural. I have tried various combinations of namespace and using, but to no avail.

Any suggestions ???

+2  A: 

I don't think it can be done. AFAIK, you can use using appearance::color in another class or structure as stipulated here.

A using declaration in a class A may name one of the following:

  • A member of a base class of A

  • A member of an anonymous union that is a member of a base class of A

  • An enumerator for an enumeration type that is a member of a base class of A

Jacob
+1  A: 

I don't think you can do this with a class-scoped enum. Probably the only way to achieve something similar is to enclose the enum in its own distinct namespace and then use that where needed to being in the enum.

EDIT: In this question http://stackoverflow.com/questions/3293279/how-do-you-import-an-enum-into-a-different-namespace-in-c I show one possible way to import an enum from one namespace into another, but I don't believe it would work (as-is anyway) for this class case.

Mark B
+1  A: 

As Jacob said you can't do this directly, but you can make it work by encapsulating the enum within it's own namespace.

namespace enums{
        enum color
        {BLUE
        ,RED
        ,GREEN};
} // namespace enums


using namespace enums;
class Foo
{
    int Bar(){return BLUE;}
}

something like that should work...

Kyle C
The OP mentioned "I would like to keep the enum within the class{}".
casablanca