C++ 如何在代码的另一部分使用类中的 Enum 值?

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

How do I use the Enum value from a class in another part of code?

c++classenums

提问by ChiggenWingz

Coming from a C# background from a night course at a local college, I've sort of started my way in C++. Having a lot pain getting used to the syntax. I'm also still very green when it comes to coding techniques.

来自当地大学夜校的 C# 背景,我已经开始学习 C++。习惯语法很痛苦。在编码技术方面,我仍然非常绿色。

From my WinMainfunction, I want to be able to access a variable which is using an enum I declared in another class.

从我的WinMain函数中,我希望能够访问使用我在另一个类中声明的枚举的变量。

(inside core.h)
class Core
{
    public:
    enum GAME_MODE
    {
        INIT,
        MENUS,
        GAMEPLAY
    };
    GAME_MODE gameMode;

    Core();
    ~Core();
    ...OtherFunctions();
};

(inside main.cpp)
Core core;
int WINAPI WinMain(...)
{
    ... startup code here...

    core.gameMode = Core.GAME_MODE.INIT;

    ...etc...
}

Basically I want to set that gameMode to the enum value of Initor something like that from my WinMainfunction. I want to also be able to read it from other areas.

基本上,我想将该 gameMode 设置Init为我的WinMain函数中的枚举值或类似值。我也希望能够从其他领域阅读它。

I get the error...

我收到错误...

expected primary-expression before '.' token

If I try to use core.gameMode = Core::GAME_MODE.INIT;, then I get the same error.

如果我尝试使用core.gameMode = Core::GAME_MODE.INIT;,则会出现相同的错误。

I'm not fussed about best practices, as I'm just trying to get the basic understanding of passing around variables in C++ between files. I'll be making sure variables are protected and neatly tucked away later on once I am use to the flexibility of the syntax.

我对最佳实践并不大惊小怪,因为我只是想对在 C++ 中的文件之间传递变量有基本的了解。一旦我习惯了语法的灵活性,我将确保变量受到保护并整齐地隐藏起来。

If I remember correctly, C# allowed me to use Enums from other classes, and all I had to do was something like Core.ENUMNAME.ENUMVALUE.

如果我没记错的话,C# 允许我使用来自其他类的枚举,而我所要做的就是像Core.ENUMNAME.ENUMVALUE.

I hope what I'm wanting to do is clear :\ As I have no idea what a lot of the correct terminology is.

我希望我想要做的很清楚:\因为我不知道很多正确的术语是什么。

回答by Martin B

Use

core.gameMode = Core::INIT;

The individual values of an enumeration are scoped not within that enumeration but at the same level as the enumeration itself. This is something that most other languages (including C#) do differently, and C++0x will allow both variants so that there,

枚举的各个值的范围不在该枚举内,而是与枚举本身处于同一级别。这是大多数其他语言(包括 C#)做的不同的事情,C++0x 将允许这两种变体,因此,

core.gameMode = Core::GAME_MODE::INIT;

will also be legal.

也将是合法的。

In addition, the strongly typed enums that will be added in C++0x (enum class) will put the enum values onlywithin the scope of the enum (i.e. as in C#); this solves the problem you noted in your comment that for "normal" enums, the identifiers for enum values need to be unique across all enums defined in the same scope.

此外,C++0x( enum class) 中将添加的强类型枚举将将枚举值放在枚举的范围内(即在 C# 中);这解决了您在评论中指出的问题,即对于“普通”枚举,枚举值的标识符在同一范围内定义的所有枚举中必须是唯一的。