Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumeration Scope

Tags:

c

enums

If I have enums like:

enum EnumA
{
  stuffA = 0
};
enum enumAA
{
  stuffA = 1
};

What happens here when you refer to stuffA? I thought you would refer to them like EnumA.stuffA and EnumB.stuffA as in Java, but that doesn't seem to be the case in C.

like image 512
prelic Avatar asked Dec 09 '11 15:12

prelic


People also ask

What is a scoped enumeration?

Scoped enums (enum class/struct) are strongly typed enumerations introduced in C++11. They address several shortcomings of the old C-style (C++98) enums, mainly associated with type-safety and name collisions.

What is the purpose of enumeration?

Enumerations make for clearer and more readable code, particularly when meaningful names are used. The benefits of using enumerations include: Reduces errors caused by transposing or mistyping numbers. Makes it easy to change values in the future.

What is enumeration explain with example?

An enumeration is used in any programming language to define a constant set of values. For example, the days of the week can be defined as an enumeration and used anywhere in the program. In C#, the enumeration is defined with the help of the keyword 'enum'.

What is enumeration in data structure?

An enumeration is a data type that consists of a set of named values that represent integral constants, known as enumeration constants. An enumeration is also referred to as an enumerated type because you must list (enumerate) each of the values in creating a name for each of them.


1 Answers

enums don't introduce new scope.

In your example, the second enum wouldn't compile due to the stuffA name clash.

To avoid name clashes, it is a common practice to give the elements of an enum a common prefix. Different prefixes would be used for different enums:

enum EnumA
{
  EA_stuffA = 0
};
enum EnumAA
{
  EAA_stuffA = 1
};
like image 132
NPE Avatar answered Sep 21 '22 09:09

NPE