C++ 你应该在类内部还是外部声明枚举?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9630744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:05:52  来源:igfitidea点击:

Should you declare enums inside or outside a class?

c++coding-styleenumsnamespaces

提问by aCuria

Should you declare enums inside or outside a class if the said enums are only used in the class member functions?

如果所述枚举仅在类成员函数中使用,您应该在类内部还是外部声明枚举?

namespace nspace
{

// need to append OC, as this pollutes the current namespace
enum OUTSIDE_CLASS {OC_POINTS, OC_LINES, OC_LINE_LOOP, :::};
enum OTHER_ENUM {OE_POINTS};
class VertexBuffer
{
public:
    enum INSIDE_CLASS {POINTS, LINES, LINE_LOOP, :::};
    void foo(OUTSIDE_CLASS e);
    void bar(INSIDE_CLASS e);
}
};

// usage
nspace::VertexBuffer v;
v.foo(nspae::VB_POINTS);
v.bar(nspace::VertexBuffer::POINTS); // more pedantic

回答by Matthieu M.

The real goal is to avoid polluting the scope (either global or namespace) and help grouping related values together (works pretty goods with autocompletion in IDE).

真正的目标是避免污染范围(全局或命名空间)并帮助将相关值组合在一起(在 IDE 中使用自动完成功能非常好)。

With C++11, you can declare strongly typed enums using:

使用 C++11,您可以使用以下方法声明强类型枚举:

enum class MyEnum {
  Value0,
  Value1
};

which are necessarily invoked as MyEnum::Value0(and not Value0).

必须作为MyEnum::Value0(而不是Value0)调用。

In C++03, you can more or less emulate this with:

在 C++03 中,您可以或多或少地模拟这一点:

struct MyEnum {
  enum Type {
    Value0,
    Value1
  };
};

But then the type of the enum is MyEnum::Typewhich is subtly different.

但是,枚举的类型MyEnum::Type略有不同。

The lazy option is to just dump it in a class, but I still favor nesting a scoped enum, even within a class, just to make it clear that those values are not loosebut instead are inter-related.

懒惰的选择是将它转储到一个类中,但我仍然喜欢嵌套一个作用域枚举,即使在一个类中,只是为了明确这些值不是松散的,而是相互关联的

回答by Alok Save

If only your class members use the enumit is preferable to declare the enuminside the class.
This prevents the namespace/global space from pollution due to unneeded symbol names & also
It is more intutive for users of the class, it helps the user to know that the enumwill only be used by the class.

如果只有您的班级成员使用enum,则最好enum在班级内部声明。
这可以防止命名空间/全局空间因不需要的符号名称而受到污染
,而且对于类的用户来说更直观,它可以帮助用户知道enum只会被类使用。

The general rule you should follow is:
Do not add any symbol in a scope(global/namespace) which will not be accessed(& hence not needed) in that scope.

您应该遵循的一般规则是:
不要在范围(全局/命名空间)中添加任何不会在该范围内访问(因此不需要)的符号。

回答by Maxim Egorushkin

As Matthieu M. mentioned, in C++11 use Strongly typed enumerations.

正如 Matthieu M. 提到的,在 C++11 中使用Strongly typed enumerations

A good way to emulate them in C++03 is to wrap enums in namespaces. It is better than wrapping in a structbecause that namespace may have functions that can be found using argument-dependent name lookup. E.g.:

在 C++03 中模拟它们的一个好方法是将枚举包装在命名空间中。它比包装在 a 中更好,struct因为该命名空间可能具有可以使用依赖于参数的名称查找来找到的函数。例如:

namespace MyEnum {
  enum Type {
    Value0,
    Value1
  };

  std::ostream& operator<<(std::ostream&, Type);
  std::istream& operator>>(std::istream&, Type&);
}

MyEnum::Type value;
std::cout << value; // uses the overload from MyEnum
std::cin >> value; // uses the overload from MyEnum

回答by Cheers and hth. - Alf

It depends. Strive to minimize exposure of implementation details. E.g. enclose things in namespaces and/or classes and/or separately compiled implementation files. And as that hopefully makes very clear, the global namespace and in-class are absolutely not the only options. I.e., declaring something in-class is not the only way to minimize exposure (what are some other ways then? --- oh, well, remember the third sentence of this answer).

这取决于。尽量减少实施细节的暴露。例如,将事物包含在命名空间和/或类和/或单独编译的实现文件中。正如希望非常清楚的那样,全局命名空间和类内绝对不是唯一的选择。即,在课堂上宣布某事并不是减少暴露的唯一方法(那么还有哪些其他方法?---哦,好吧,记住这个答案的第三句话)。