Wrap legacy c enum to c++11 enum class

1k views Asked by At

I have an old C library and I have to wrap it inside a C++ environment. I use C++11 enum class because they're so useful and I'd like to do transform the original enum into a new enum class without simply create the new enum class and rewriting it. I'd like to do something like a typedef as in the old C:

enum num_oldc
{
    one,
    two,
    three
};

typedef enum num_oldc num_oldc_t;

But one_t must be accessible via its scope num_t::two exactly in the same way I can access a new C++11 enum class.

1

There are 1 answers

1
Timothy Shields On

You won't be able to do this with a typedef. However, you can do it as follows.

  • Create your nicer enum class with entries that correspond to the entries of your C-style enum.
  • Create two functions for mapping between your enum class and the C-style enum.
  • In your wrapper API, use only the enum class.
  • In the implementation of your wrapper, convert to and from the C-style enum whenever you need to interface with the library you are wrapping.