Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a non static member function from a static member function without passing class instance

I need to call a non static member function from a static member function of the same class. The static function is a callback. It can receive only void as data, though which i pass a char*. So i cannot directly provide the class instance to the callback. I can pass a structure instead of char to the callback function. Can anyone give eg code to use the non static member function in a static member function . and use the structure in the static member function to use the instance of the class to call the non static member function?

like image 512
HariHaraSudhan Avatar asked Aug 01 '11 06:08

HariHaraSudhan


People also ask

Can a static member function call non static member function of a class?

A class can have non-static member functions, which operate on individual instances of the class. class CL { public: void member_function() {} }; These functions are called on an instance of the class, like so: CL instance; instance.

What happens if non static members are used in static member function?

What happens if non static members are used in static member function? Explanation: There must be specific memory space allocated for the data members before the static member functions uses them. But the space is not reserved if object is not declared.

How do you call a function from a static function?

By declaring a function member as static, you make it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::.

How do you call a non static method from a static method in C++?

A static method provides NO reference to an instance of its class (it is a class method) hence, no, you cannot call a non-static method inside a static one. Create an object of the class inside the static method and then call the non-static method using such an object.


1 Answers

Normally 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, data points to an instance of your class. E.g.

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

Now, if I understand you correctly, you need to also pass a char*. You can either wrap both in a struct and unwrap it in the callback like so:

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 modify the definition of CMyClass, put all the necessary data in class members, so that you can use a callback as in the first example.

like image 172
Henrik Avatar answered Sep 22 '22 17:09

Henrik