I try to access in a C program data received from a C++ library which has been originally designed with structure inheritance:
example:
// C++ data structures
typedef struct _Base
{
public:
int id;
wchar_t* name;
} Base;
typedef struct _Struct1 : Base
{
public:
int valueCount;
} Struct1;
typedef struct _Struct2 : Base
{
public:
int parentID;
int amount;
} Struct2;
I tried using the following data structures in C for mapping.
typedef struct _Base
{
int id;
wchar_t* name;
} Base;
typedef struct _Struct1
{
// Base struct data
int id;
wchar_t* name;
int valueCount;
} Struct1;
typedef struct _Struct2
{
// Base struct data
int id;
wchar_t* name;
int parentID;
int amount;
} Struct2;
But printing the data, it looks like I get wrong values.
Am I missing something, any reference on how C++ represents inherited structure internally?
Thanks in advance!
The C++11 rules on PODs (What are Aggregates and PODs and how/why are they special?)
specify thatdon’t allow mixing concrete base classes with data members, but in practice for most compilers having a single POD base class is equivalent to encapsulating that class as the first member.Try specifying your C structs encapsulating the base struct:
Note that this won’t work if the C++ classes are non-POD (e.g. have virtual methods).