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:
It's not a problem per-se, but you might violate the strict aliasing rule if and when you cast that void *
back to Mgr::Obj *
in your C++ code.
There's no good solution to this - it is (to me) a glaring omission in the standard. The best you can do is to compile any code which does such casts with the -fno-strict-aliasing
flag. I do this extensively and have never had a problem performing casts in this manner.