导出类的部分命名空间
我有一个包含 enum
的类:
class appearance{
// ... stuff ...
enum color {BLUE, RED, GREEN};
};
我想附加部分命名空间(使用 using
),以便我可以引用 BLUE 的值
简单地表示为 BLUE
,而不是 appearance::BLUE
。同时,我希望将 enum
保留在 class{}
中,因为我认为这是最自然的。我尝试了 namespace
和 using
的各种组合,但没有成功。
有什么建议吗???
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 ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这是不可能的。 AFAIK,您可以按照规定在另一个类或结构中使用
using Appearance::color
此处。I don't think it can be done. AFAIK, you can use
using appearance::color
in another class or structure as stipulated here.我认为你不能用类范围的枚举来做到这一点。实现类似功能的唯一方法可能是将枚举包含在其自己的不同名称空间中,然后在需要的地方使用枚举。
编辑:在这个问题中 你怎么样将枚举导入到 C++ 中的不同命名空间中? 我展示了一种将枚举从一个命名空间导入另一个命名空间的可能方法,但我不认为它对于此类情况有效(无论如何)。
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.
正如 Jacob 所说,您不能直接执行此操作,但可以通过将枚举封装在其自己的命名空间中来使其工作。
类似的东西应该有效......
As Jacob said you can't do this directly, but you can make it work by encapsulating the enum within it's own namespace.
something like that should work...