Listing Area

If I have listings like:

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

What happens here when you refer to stuffA? I thought that you will refer to them as EnumA.stuffAand EnumB.stuffAin Java, but in C. it is not.

+9
source share
6 answers

enums Do not enter a new area.

In your example, the second enumwill not compile due to a conflict stuffA.

To avoid name conflicts, it is common practice to provide elements with a enumcommon prefix. For different enumerations, different prefixes will be used:

enum EnumA
{
  EA_stuffA = 0
};
enum EnumAA
{
  EAA_stuffA = 1
};
+9
source

(, , , /), stuffA.

( , ) .

+7

, , . , , . .

enum EnumA
{
  stuffA = 0
};

void func(void) {
   enum enumAA
   {
     stuffA = 1
   };
   // do something
}

. , .

+6

, , stuffA . Enum ( "stuffA", EnumA.stuffA). , (, ). ints, , .

+1

, C 2018 . , , .

6.2.3, " " :

- , , . , , :

...

- , ( ).

, . ( , , [ goto ]; , [ struct, struct foo ]; [ ]).

6.7 "" 5, :

- , :

...

() ;

...

, , . , 6.2.1, " ", 1:

; ; , ; ; ; ; . . .

, , foo , - . enum A enum B , :

enum A { foo = 1 };
enum B { foo = 1 };

, foo , foo enum A, , , enum B

( , , . : , , . , .)

0
source

Depending on where you declare these enumerations, you can also declare new areas using the namespace keyword.

NOTE: I would not advise doing this, I just note that this is possible. Instead, it would be better to use a prefix, as noted in other examples.

namespace EnumA
{
    enum EnumA_e
    {
        stuffA = 0
    };
};

namespace EnumAA
{
    enum enumAA_e
    {
        stuffA = 1
    };
};
-1
source

Source: https://habr.com/ru/post/1648692/


All Articles