The usual way is to create some c++ function that can be called from c and returns a void*
handle for the class allocated internally:
extern "C" {
void* createScrnDev() {
return new scrnDev();
}
}
Other functions in this wrapper API would take the handle, and cast it back to the original object instance:
extern "C" {
void blink(void* scrnDev_) {
static_cast<scrnDev*>(scrnDev_)->blink();
}
}
Finally you'll need a destroy()
function to call delete
properly:
extern "C" {
void destroyScrnDev(void* scrnDev_) {
delete static_cast<scrnDev*>(scrnDev_);
}
}
As mentioned in comments you can also use an opaque pointer, that uses a struct
declaration used in both languages:
scrnDev.h
:
struct scrnDevHandle;
scrnDevHandle* createScrnDev();
void blink(scrnDevHandle* scrnDev_);
void destroyScrnDev(scrnDevHandle* scrnDev_);
scrnDev.cpp
:
class scrnDev : public scrnDevHandle {
public:
scrnDev() {}
blink() {
// ...
}
};
extern "C" {
scrnDevHandle * createScrnDev() {
return new scrnDev();
}
void blink(scrnDevHandle* scrnDev_) {
static_cast<scrnDev*>(scrnDev_)->blink();
}
void destroyScrnDev(scrnDevHandle* scrnDev_) {
delete static_cast<scrnDev*>(scrnDev_);
}
}
You might be interested to read some more details about the extern "C" {}
interfacing here