C++ 枚举:他们可以在 .h 中完成还是必须留在 .cpp 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1284529/
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 19:26:59  来源:igfitidea点击:

Enums: Can they do in .h or must stay in .cpp?

c++header

提问by user147502

If I have something like:

如果我有类似的东西:

enum
{
    kCP_AboutBox_IconViewID = 1,
    kCP_AboutBox_AppNameViewID = 2,
    kCP_AboutBox_VersionViewID = 3,
    kCP_AboutBox_DescriptionViewID = 4,
    kCP_AboutBox_CopyrightViewID = 5
};

in my .cpp can it go in the .h?

在我的 .cpp 中它可以进入 .h 吗?

More so, what other lesser know things can you put in a .h besides class definitions, variables, etc, etc

更重要的是,除了类定义、变量等之外,您还可以在 .h 中放入哪些鲜为人知的东西

回答by IAmFledge

A .h file is essentially just code which, at compile time, is placed above any .cpp (or .h file for that matter) that it's included in. Therefore you CAN just place any code from the .cpp file into the .h and it should compile fine.

.h 文件本质上只是代码,在编译时,它位于包含它的任何 .cpp(或 .h 文件)之上。因此,您可以将 .cpp 文件中的任何代码放入 .h它应该可以正常编译。

However it's the design which is important. Your code (e.g. your enum) SHOULD be placed in the .h file if you need to expose it to the code you're including the .h file. However if the enum is only specific to the code in your header's .cpp implementation, then you should encapsulate it just within the .cpp file.

然而,重要的是设计。如果您需要将代码(例如您的枚举)暴露给包含 .h 文件的代码,则应将其放置在 .h 文件中。但是,如果枚举仅特定于标头的 .cpp 实现中的代码,那么您应该将其封装在 .cpp 文件中。

回答by quamrana

Remember to use header include guards in headers like:

请记住在标头中使用标头包含保护,例如:

#ifndef header_name_h
#define header_name_h
...
#endif

This helps you to keep to the one definition rule when multiple headers include your header.

当多个标题包含您的标题时,这有助于您遵守一个定义规则。

Update:

更新:

I have since found that the latest versions of Visual Studio andgcc both allow:

我发现最新版本的 Visual Studiogcc 都允许:

#pragma once

Also, Never Ever have:

此外,从来没有:

using namespace <name>;

in a header as this can cause strange ambiguity problems.

在标题中,因为这会导致奇怪的歧义问题。

回答by mipadi

Yes, your enum definition can go in your header (.h) file. Don't repeat the definition in your .cppfile, though.

是的,您的枚举定义可以放在您的头 ( .h) 文件中。但是,不要在.cpp文件中重复定义。

回答by Zeroshade

In general an enum is going to be used as a type definition and should always be in the header file. Things to think about are the scope of it.

通常,枚举将用作类型定义,并且应始终位于头文件中。要考虑的事情是它的范围。

If the enum is just placed outside any scope in the header, it will be globally available to any thing that includes the header file. If you instead want the enum only accessible to the class itself, you can place it in the private section of the class.

如果枚举只是放在头文件中的任何范围之外,那么它对于包含头文件的任何事物都是全局可用的。如果您希望枚举只能由类本身访问,您可以将它放在类的私有部分。

In general you shouldn't make the enum globally scoped, instead you should either put it in a namespace or in the public section of a class. Then you can access the enum by way of

通常,您不应将枚举设为全局范围,而应将其放在命名空间或类的公共部分中。然后您可以通过以下方式访问枚举

NamespaceOrClass::EnumValue

Also, as a sidenote, enums automatically iterate values from the first one you give (or 0).

此外,作为旁注,枚举会自动从您提供的第一个值(或 0)开始迭代值。

enum
{
    kCP_AboutBox_IconViewID = 1,
    kCP_AboutBox_AppNameViewID = 2,
    kCP_AboutBox_VersionViewID = 3,
    kCP_AboutBox_DescriptionViewID = 4,
    kCP_AboutBox_CopyrightViewID = 5
};

Is exactly the same as

完全一样

enum
{
    kCP_AboutBox_IconViewID = 1,
    kCP_AboutBox_AppNameViewID,
    kCP_AboutBox_VersionViewID,
    kCP_AboutBox_DescriptionViewID,
    kCP_AboutBox_CopyrightViewID
};

It's not a problem or error, just stylistic really.

这不是问题或错误,只是风格而已。

回答by nobody

Yes, of course you can put it in a .h file. The only things that shouldn't go in a .h file are things that would cause a problem if they get included into more than one object, such as global object initializers.

是的,当然你可以把它放在一个 .h 文件中。.h 文件中唯一不应该包含的内容是如果它们包含在多个对象中会导致问题的内容,例如全局对象初始值设定项。

回答by Johannes Schaub - litb

The one definition rule allows that at 3.2/5. All of the following can be put into headers, and be included multiple times into different translation units.

one 定义规则允许在3.2/5. 以下所有内容都可以放入标题中,并多次包含在不同的翻译单元中。

There can be more than one definition of a class type (clause 9), enumeration type(7.2), inline function with external linkage (7.1.2), class template (clause 14), non-static function template (14.5.5), static data member of a class template (14.5.1.3), member function of a class template (14.5.1.1), or template specialization for which some template parameters are not specified (14.7, 14.5.4) in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements.

类类型(第 9 条)、枚举类型(7.2)、具有外部链接的内联函数(7.1.2)、类模板(第 14 条)、非静态函数模板(14.5.5)可以有多个定义、类模板的静态数据成员 (14.5.1.3)、类模板的成员函数 (14.5.1.1) 或在程序中未指定某些模板参数的模板特化 (14.7, 14.5.4),前提是每个定义出现在不同的翻译单元中,并且定义满足以下要求。

The requirements essentially come down to that each definition must be the same. Note that if your enumeration type itself has no name, then it's not covered by that rule. Each definition of it in another translation unit defines a new enumeration type then, and doesn't clash with each other.

要求基本上归结为每个定义必须相同。请注意,如果您的枚举类型本身没有名称,则该规则不涵盖它。它在另一个翻译单元中的每个定义都定义了一个新的枚举类型,并且不会相互冲突。

Putting them into the header is a good place if it's supposed to be public. Putting them in the implementation file is a good place if it's supposed to be private to that single file. In the latter case, either put them into an unnamed namespace, or make them unnamed (like it's the case with your enumeration example), so that it can't clash with another enumeration having the same name.

如果它应该是公开的,将它们放入标题是一个好地方。如果它应该是该单个文件的私有文件,那么将它们放在实现文件中是一个好地方。在后一种情况下,要么将它们放入未命名的命名空间,要么使它们未命名(就像您的枚举示例的情况),这样它就不会与具有相同名称的另一个枚举发生冲突。

回答by Matthieu

I have not enough information, but maybe you can declare the enum not only in the .h but inside the class. Remember to keep the scope of a variable to a minimum.
if the enum is relevant to a particular class you should declare it inside the class.

我没有足够的信息,但也许您不仅可以在 .h 中而且可以在类中声明枚举。请记住将变量的范围保持在最小。
如果枚举与特定类相关,您应该在类中声明它。