Best way to map/join two autogenerated enums

Posted by tomlip on Stack Overflow See other posts from Stack Overflow or by tomlip
Published on 2013-10-22T15:50:44Z Indexed on 2013/10/22 15:53 UTC
Read the original article Hit count: 224

Filed under:
|
|

What is the best C++ (not C++11) way of joining two enums from autogenerated class similar to one presented below:

namespace A {
  namespace B { ...
    class CarInfo {
      enum State {   // basically same enums defined in different classes
        Running,
        Stopped,
        Broken
      }
    }

    class BikeInfo {
      enum State {   // basically same enums defined in different classes
        Running,
        Stopped,
        Broken
      }
    }
  }
}

What is needed is unified enum State for both classes that is seen to outside world alongside with safe type conversion.

The best and probably most straightforward way I came up with is to create external enum:

enum State {
  Running,
  Stopped,
  Broken
}

together with conversion functions

State stateEnumConv(A::B::CarInfo::State aState);
State stateEnumConv(A::B::BikeInfo::State aState);
A::B::CarInfo::State stateEnumConv(State aState);
A::B::BikeInfo::State stateEnumConv(State aState);

Direction into right approach is needed.

Gosh coming from C I hate those long namespaces everywhere an I wish it could be only A::B level like in presented example. Four conversion functions seem redundant note that CarInfo::State and BikeInfo::State has same enum "members".

© Stack Overflow or respective owner

Related posts about c++

Related posts about enums