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?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
This shows that present design is flawed or inproper. IMHO, you should rather think of changing the design. Just imagine if you somehow get the things working but what about the maintainability an readability of the code.
I would suggest that you should change your callback function to different signature and made according changes.
This is the only way :
Normally such a callback would look like this:
Of course, you need to make sure, data points to an instance of your class. E.g.
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:
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.
If your instance is a singleton (usually implemented using a private or protected constructor and a static pointer to itself) you can do e.g.:
If you don't use a singleton but you can pass the instance cast to a
void *
then you can do this instead: