简体   繁体   English

如何使用C#等C ++枚举类型?

[英]How can I use C++ enum types like C#?

How can I use C++ enum types like C#? 如何使用C#等C ++枚举类型?

consider following definition in c++ : 考虑c ++中的以下定义:

enum myEnum { A, B, C};
myEnum en = A;

Now I want to write line #2 as following line like C# : 现在我想把第2行写成如C#的以下行:

myEnum en = myEnum.A;

?? ??

Well, different rules in C++. 好吧,C ++中有不同的规则。 Closest you could get is this slight abomination: 你能得到的最近的是这种轻微的憎恶:

namespace myEnum {
    enum myEnum { A, B, C};
}

myEnum::myEnum en = myEnum::A;

C++0x introduces enum class that does exactly what you want: C ++ 0x引入的enum class完全符合您的要求:

enum class myEnum { A, B, C };
myEnum en = myEnum::A;

In C, I would probably use good old prefixing: 在C中,我可能会使用旧的前缀:

enum myEnum { myEnum_A, myEnum_B, myEnum_C };
myEnum en = myEnum_A;

What exactly is the question here? 这究竟是什么问题? Your C# example will work as you have written it (although it doesn't match the .NET naming conventions...) 您的C#示例将在您编写它时起作用(尽管它与.NET命名约定不匹配......)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM