Is case statement1 + statement2: bad encoding?

I wrote a switch statement that looks like this:

switch(statement){
    case statement1:
        break;
    case statement2:
        break;
    case statement3:
        break;
    case statement4:
        break;
    case statement1 + statement2:
        break;
    case statement1 + statement3:
        break;
    case statement1 + statement4:
        break;
    default:
        break;
}

with instructions defined in a header like this:
  #define statement1 999
  #define statement2 32898
  #define statement3 32899
  #define statement4 32900
This compiles and works fine using visual studio 2003.

For me, this gives more statements with the need to define more variables. Let me have different options depending on which statements were selected in the previous code.

How does the community perceive this code? Accepted? Bad practice? Can be avoided?

The only risk that I see is when there is a definition of 33897,33898,33899.

+4
source share
2 answers

, , , .

-, . constexpr, . constexpr.

:

const int statement1 = 999;
const int statement2 = 32898;
const int statement3 = 32899;
const int statement4 = 32900;

constexpr, ++ 11.

, . ints, .

, , . , constexpr, , , .

switch , , , , , - 33897, 33898, 33899. . 33897 , , :

const int combine2 = statement1 + statement2;
const int combine3 = statement1 + statement3;

, , .

+3

#define. , .

constexpr:

constexpr int statement1 = 2;

. .

+3

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


All Articles