导出类的部分命名空间

发布于 2024-09-27 03:52:41 字数 409 浏览 1 评论 0原文

我有一个包含 enum 的类:

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

我想附加部分命名空间(使用 using),以便我可以引用 BLUE 的值 简单地表示为 BLUE,而不是 appearance::BLUE。同时,我希望将 enum 保留在 class{} 中,因为我认为这是最自然的。我尝试了 namespaceusing 的各种组合,但没有成功。

有什么建议吗???

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晚雾 2024-10-04 03:52:41

我认为这是不可能的。 AFAIK,您可以按照规定在另一个类或结构中使用 using Appearance::color 此处

A 类中的 using 声明可以
说出以下其中一项:

  • A 基类的成员

  • 匿名联合的成员,该联合是 A 基类的成员

  • 作为 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

原来分手还会想你 2024-10-04 03:52:41

我认为你不能用类范围的枚举来做到这一点。实现类似功能的唯一方法可能是将枚举包含在其自己的不同名称空间中,然后在需要的地方使用枚举。

编辑:在这个问题中 你怎么样将枚举导入到 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.

忆离笙 2024-10-04 03:52:41

正如 Jacob 所说,您不能直接执行此操作,但可以通过将枚举封装在其自己的命名空间中来使其工作。

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


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

类似的东西应该有效......

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文