开发者

Export part of the namespace of a class

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 ca开发者_JAVA百科n 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 ???


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


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 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.


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...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜