I have a nested class in C++ that I want C code to be able to use. Because it is nested I cannot forward declare it to C, so instead we have code like this in a shared header file:
#ifdef __cplusplus
class Mgr {
public:
class Obj {
public:
int x;
};
};
typedef Mgr::Obj * PObj;
#else
typedef void* PObj;
#endif
This results in C and C++ each seeing a different definitions of PObj which I fear violates the one definition rule. However it is "just" a pointer, so I'm not sure if this can go wrong or not.
C does nothing with the pointer besides pass it along to C++ functions which are wrappers for methods. For instance we'll have this in the combined header:
#ifdef __cplusplus
extern "C" {
#endif
int obj_GetX(PObj pObj);
#ifdef __cplusplus
}
#endif /* __cplusplus */
With this as the implementation in the C++ file:
int obj_GetX(PObj pObj) {
return pObj->x;
}
So:
- Is this really a problem?
- If so, what is the best way to share this pointer between C and C++