Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Enum Names Overlap

Tags:

c++

enums

c++11

Lets say I have a class which contains two enums.

    enum NameType { Defined, Inherited };
    enum ValueType { Defined, Inherited };

The numeration values are said to be "previously defined" in the second enum because they have the same names as the first enum. Is there a syntax that is concise and allows for these names?

I know in C# this isn't a problem, but in C++ it seems to be. I'm using C++11.

like image 715
Russell Trahan Avatar asked Jun 18 '26 15:06

Russell Trahan


1 Answers

Since you're using C++11, I'd recommend using enum class.

enum class NameType { Defined, Inherited };
enum class ValueType { Defined, Inherited };

This fixes the scoping issue.

See http://www.cprogramming.com/c++11/c++11-nullptr-strongly-typed-enum-class.html for further discussion of enum class.

like image 111
NPE Avatar answered Jun 21 '26 04:06

NPE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!