c++ - How to fill a function pointer member with class instance member function pointer? -
having c api need create c++ object extends so:
struct oldcinterface { void (*dostuff)(); }; struct newcxxbaseclass : protected oldcinterface { virtual void dostuff(); virtual ~newcxxbaseclass(); }; how put instance pointer cxx dostuff c interface in constructor if possible having pointing overload if have base child?
void (*dostuff)() if function pointer, while &newcxxbaseclass::dostuff of type void (newcxxbaseclass::*)(), is, pointer member function. not compatible. usually, not of same size (sizeof(void (*)()) != sizeof(void (newcxxbaseclass::*)())). saying so, following correct
struct oldcinterface { void (newcxxbaseclass::*dostuff)(); }; but since original intention work old c code. not work. best best make static member function calls virtual member function of object. illustration:
struct proxy { static newcxxbaseclass* s_p_; static void dostuff() { s_p_->dostuff(); } }; &proxy::dostuff of type void(*)() , can used function pointer expected. then, can following:
oldcinterface c_interface = {&proxy::dostuff};
Comments
Post a Comment