Behrouz.M
Behrouz.M

Reputation: 3573

How can I use C++ enum types like C#?

How can I use C++ enum types like C#?

consider following definition in c++ :

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

Now I want to write line #2 as following line like C# :

myEnum en = myEnum.A;

??

Upvotes: 3

Views: 995

Answers (3)

fredoverflow
fredoverflow

Reputation: 263360

C++0x introduces enum class that does exactly what you want:

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

In C, I would probably use good old prefixing:

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

Upvotes: 7

Hans Passant
Hans Passant

Reputation: 942518

Well, different rules in C++. Closest you could get is this slight abomination:

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

myEnum::myEnum en = myEnum::A;

Upvotes: 8

thecoop
thecoop

Reputation: 46168

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...)

Upvotes: 0

Related Questions