I have C code that calls C++ code. The C++ code creates an object and then passes it back to the C code, which stores the object in a struct:
extern "C" void cppFn(?** objectPtr)
{
*objectPtr = new Object();
}
void cFn()
{
THESTRUCT theStruct = {0};
cppFn(&(theStruct.objectPtr));
}
typedef struct THESTRUCT
{
?* objectPtr;
} THESTRUCT;
My question: what is the accepted type to use for objectPtr?
void. Like so:void*is a “generic” pointer type. (You have to cast it to some other type to use it. Since there is no type to cast it to at the C end, it’s effectively an opaque pointer.)Another approach is to make a forward declaration for your C++ type, without defining it (since that’s impossible to do at the C end). So if your C++ type is called
foo, you could do: