How to call a non-stationary member function from a static member function without passing an instance of the class

I need to call a non-stationary member function from a static member function of the same class. A static function is a callback. It can only receive void as data, although I pass char *. Therefore, I cannot directly provide an instance of the class for the callback. I can pass a structure instead of a char callback function. Can someone give, for example, code to use a non-stationary member function in a static member function. and use a structure in a static member function to use an instance of a class to call a non-stationary member function?

+4
source share
4 answers

Typically, such a callback would look like this:

void Callback( void* data) { CMyClass *myClassInstance = static_cast<CMyClass *>(data); myClassInstance->MyInstanceMethod(); } 

Of course, you need to make sure that the data points to an instance of your class. For instance.

 CMyClass* data = new CMyClass(); FunctionCallingMyCallback( data, &Callback); delete data; 

Now, if I understand you correctly, you also need to pass char *. You can either wrap both in the structure and expand it in the callback like this:

 MyStruct* data = new MyStruct(); data->PtrToMyClass = new CMyClass(); data->MyCharPtr = "test"; FunctionCallingMyCallback( data, &Callback); delete data->PtrToMyClass; delete data; void Callback( void* data) { MyStruct *myStructInstance = static_cast<MyStruct *>(data); CMyClass *myClassInstance = myStructInstance->PtrToMyClass; char * myData = myStructInstance->MyCharPtr; myClassInstance->MyInstanceMethod(myData); } 

or, if you can change the definition of CMyClass, put all the necessary data in the members of the class so that you can use the callback, as in the first example.

+5
source

If your instance is singleton (usually implemented using a private or protected constructor and a static pointer to itself), you can do, for example:

 class MyClass { private: MyClass():myInstance(0) {} MyClass *myInstance; void callback(); public: ~MyClass() {} static MyClass *getInstance(); static void myCallback(); }; MyClass *MyClass::getInstance() { if(!myInstance) { myInstance = new MyClass; } return myInsance; } void MyClass::callback() { // non-static callback } void MyClass::myCallback() { getInstance()->callback(); } 

If you are not using a singleton, but you can pass the cast instance to void * , then you can do this instead:

 void MyClass::myCallback(void *data) { MyClass *instance = static_cast<MyClass *>(data); instance->callback(); } 
+4
source

This is the only way:

 #include <iostream> #include <cassert> struct A; A *oneObj = NULL; struct A { A(){ oneObj=this; } ~A(){ oneObj=NULL; } void foo() { } static void boo() { assert( NULL != oneObj ); oneObj->foo(); } }; int main() { A onlyOne; A::boo(); } 
0
source

I need to call a non- static member function from a static member of a function of the same class . The static function is a callback. It can only get void as data, although I pass char* .

This shows that the existing design is flawed or inproper . IMHO, you should consider changing the design. Imagine if you somehow do the job, but what about maintainability of the code readability.

I suggest you change the callback function to a different signature and make changes.

 class A { //... static void CallBack (A *pObj) { // logic } }; 
0
source

All Articles