Is it possible to do the epilogue of enum?

Asked 2 years ago, Updated 2 years ago, 142 views

As far as I know, ++ or += cannot be used in enum Then, how do I translate C++ enum from beginning to end?

c++ enum

2022-09-21 19:18

1 Answers

Usually when you do e-terate,

enum Foo {
  One,
  Two,
  Three,
  Last
};

for ( int fooInt = One; fooInt != Last; fooInt++ )
{
   Foo foo = static_cast<Foo>(fooInt);
   // ...
}

It's the same way. But this is not really an etherate Two = 9 If you set a value separately, you cannot write it.

Therefore, when dealing with enum, usually use the switch statement as follows, not iterate, and

switch ( foo )
{
    case One:
        // ..
        break;
    Case Two: // intentionally fall
    case Three:
        // ..
        break;
    case Four:
        // ..
        break;
     default:
        assert( ! "Invalid Foo enum value" );
        break;
}

If you really want enum to be literal, the only way is to put the value of enum in vector


2022-09-21 19:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.