C++ 将枚举放在 cpp 程序中的何处?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/993505/
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
Where to put the enum in a cpp program?
提问by Nathan Fellman
I have a program that uses enum types.
我有一个使用枚举类型的程序。
enum Type{a,b,};
class A
{
//use Type
};
class B
{
// also use that Type
};
2 class are located in 2 different files. Should I put the type definition in a headfile or in class definition for each class?
2 个类位于 2 个不同的文件中。我应该将类型定义放在头文件中还是放在每个类的类定义中?
回答by Nathan Fellman
If the enum is going to be used in more than one .cpp file, you should put it in a header file that will be included by each. If there's a common header file, you should use that, otherwise you may as well create a new header file for this enum
如果枚举将在多个 .cpp 文件中使用,则应将其放在每个文件都包含的头文件中。如果有一个通用的头文件,你应该使用它,否则你也可以为此枚举创建一个新的头文件
回答by Nathan Fellman
You should always attempt to limit the scope of types in C++, so the enum should probably be declaread at class scope. The enum will typically belong slightly more naturally in one class than the other - lets say class A, so you put it in the declaration of A in the a.h header:
您应该始终尝试限制 C++ 中类型的范围,因此枚举可能应该在类范围内声明。枚举通常比另一个类更自然地属于一个类 - 假设类 A,因此您将它放在 ah 标头中的 A 声明中:
// a.h
class A {
public:
enum Type { a, b };
...
};
Now you need to include a.h in the header that declares B:
现在您需要在声明 B 的头文件中包含 ah:
// b.h
#include "a.h"
class B {
public:
void f( A::Type t ); // use the Type enum
...
};
回答by sly
I can see the point of Neil: it is a pet peeve for many programmers to see stuff on the global scope. otoh, imho, introducing a class just for an enum is not a good style: It is supposed to be enum not a class. However, putting the same enum list in both classes (is what you were asking) would be the worst idea: we don't want to be repeating stuff.
我能理解 Neil 的观点:对于许多程序员来说,看到全局范围内的东西是一件令人讨厌的事情。otoh,恕我直言,仅仅为枚举引入一个类不是一种好的风格:它应该是枚举而不是一个类。但是,将相同的枚举列表放在两个类中(这是您要问的)将是最糟糕的主意:我们不想重复内容。
Moreover, in most non-trivial codes, one might end up using more of such shared entities (more enums, const parameters, etc...) for implementation. So, I'd begin lumping all this into an implementation namespace (say "detail") which is a child namespace of your classes, and resides in a separate header file (say "detail.hpp"), included by all. For example:
此外,在大多数非平凡代码中,最终可能会使用更多此类共享实体(更多枚举、常量参数等)来实现。因此,我开始将所有这些都归入一个实现命名空间(比如“detail”),它是你的类的子命名空间,并驻留在一个单独的头文件(比如“detail.hpp”)中,由所有人包含。例如:
// file A.hpp
#include "foo/detail.hpp"
namespace foo {
class A
{
// accessing enum as detail::a
};
}
// file B.hpp
#include "foo/detail.hpp"
namespace foo { class B { ... }; }
// file foo/detail.hpp
namespace foo { namespace detail {
enum { a,b, ... }
const int three = 3;
// etc...
// other implementation classes etc...
}}
And "detail" is nice and clean way of warning your class users to back off from whatever's declared in there. As your code gets bigger and these implementation details start growing in number you can break the dependencies into separate header files (detail1 detail2 etc...) and still keep one "detail" namespace (something which you can not do with a "class detail" for example).
并且“细节”是警告您的类用户不要在那里声明的任何内容的好和干净的方式。随着您的代码变得越来越大并且这些实现细节的数量开始增加,您可以将依赖项分解为单独的头文件(detail1 detail2 等),并且仍然保留一个“detail”命名空间(这是您无法使用“class detail”来实现的) “ 例如)。
回答by avakar
The question is rather vague, but as a rule of thumb, you should try to minimize the redundancy in your code. Therefore, you should put the declaration of the enum to a header file.
这个问题相当模糊,但根据经验,您应该尽量减少代码中的冗余。因此,您应该将枚举的声明放在头文件中。
回答by Dolphin
It really depends on if the values are the same logical type, or if they just happen to have the same names. Would it make sense to assign an A::Type variable to a C::Type? If they are the same logical type, put them in a header that both include. To keep your build times low you probably want to put it in its own header file, but putting it in a shared header with other stuff works if you want to keep the number of files down.
这实际上取决于值是否是相同的逻辑类型,或者它们是否恰好具有相同的名称。将 A::Type 变量分配给 C::Type 是否有意义?如果它们是相同的逻辑类型,请将它们放在都包含的标题中。为了保持较短的构建时间,您可能希望将其放在自己的头文件中,但如果您想减少文件数量,则将其放在与其他内容共享的头文件中是可行的。
Another option is to put the enum in a common base class that both inherit from (this may not make sense in this case, but it is another option).
另一种选择是将枚举放在一个共同继承的基类中(在这种情况下这可能没有意义,但它是另一种选择)。