2

I have a class like this:

class TType {
 public:
 ...
     enum binary_type {
        bt_a = 0,
        bt_xyz,
        ....
        bt_ak = 10,
        ....
     };
}

and I use it in several places, also the enum:

   if(var12 == TType::bt_a ) { ....

Now I imported a C library which has exactly the same enum (same keys, same values, same size) inside one of it's headerfiles:

typedef enum data_types_e {
        bt_a = 0,
        bt_xyz,
        ....
} data_types;

How can I define the enum in the c++ class definition to use the declaration of the c headerfile? I want to continue using the enum the same way as before (TType::bt_a), and avoid copying the whole enum. Furthermore I don't wont to modify the library (otherwise a preprocessor-macro would do the trick) and I want changes made in the library also be made to the enum in my class.

Neither a typedef inside the c++ class definition nor a type alias (c++11) seem to work in this situation.

4 Answers 4

2

"How can I define the enum in the c++ class definition to use the declaration of the c headerfile?"

You can simply reuse the values from the c-style enum:

 #include "TheOtherEnum.h"

 ...
     enum binary_type {
        bt_a = ::bt_a,
        bt_xyz = ::bt_xyz,
        ....
        bt_ak = ::bt_ak,
        ....
     };

"Neither a typedef inside the c++ class definition nor a type alias (c++11) seem to work in this situation."

Yes these would work to provide the correct enum type, but you'll still need to qualify the values from the global namespace and not for nested to your class.

Sign up to request clarification or add additional context in comments.

3 Comments

Since the enums share not only keys but also the values I find this solution here as ugly as copy pasting the whole enum. What if the library extends the enum? I would have to edit my version of the enum manually. Is there a way to overcome this issue?
@Dreamcooled Sorry for disgusting you, but at least it's the only solution I know, to keep the values consistent :(. I the library extends the enum you have to extend yours as well (I'd suppose you'll get noticed about such changes).
"ugly" might be the wrong word for it. I was expecting a somewhat simpler solution, that's all.
0

You can make C++ enum dependant of C enum:

typedef enum data_types_e {
        bt_a = 0,
        bt_xyz,
        ....
} data_types;

// ...

class TType {
 public:
 ...
     enum binary_type {
        bt_a = bt_a,
        bt_xyz = bt_xyz,
        ....
     };
}

Comments

0

If possible, try renaming your class TType say, class TType_1.

//=============CLibraryFileContainingEnum.h=================
typedef enum data_types_e
{
    bt_a = 9999,
    bt_xyz
} data_types;
//==========================================================

//==========================================================
class TType_1
{
    public:
    enum binary_type
    {
        bt_a = 8878,
        bt_xyz
    };
};
namespace TType
{
    #include "CLibraryFileContainingEnum.h"
}

int main()
{
    int a = TType::bt_a; //this prints 9999
    cout << a << endl;
    return 0;
}
//==========================================================

Comments

0

Here is my answer, this is an old topic, but better lately than never

class TType {
public :
  public:
  ...
  #undef __cplusplus
  #include "yourheaderc.h"
  #define __cplusplus
}

maybe with a typedef binary_type data_types to preserve your nominations

But this solution is available if your header don't contains syntax C forbidden in C++. I'm currently searching a new solution because there are prototypes in my header that contains something like :

void afunction(  unsigned long id,
                 enum T_TYPE type,
                 enum T_CHAR characteristic,
                 unsigned short number,
                 unsigned short value);

because T_TYPE and T_CHAR are not typedef-ed but this syntax is non-sense in C++ because it's the declaration syntax. So my solution is not appropriate if you are in a similar case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.