Hi everyone,
if I run a thread, how can I reference to the non static variable outside the function?
Hi everyone,
if I run a thread, how can I reference to the non static variable outside the function?
"Expose" a pointer to that variable.
gg
example ? what do you mean by expose ?
Pass the variable to the static function.
When you create a thread, you can usually pass a generic void* pointer to the thread function; so just pass a pointer to your object (maybe by passing the this pointer)...
Yes and the actual thread function that receives this pointer should simply be a tiny stub function that calls a member function on that pointer to do the real work. That way you don't end up with a function that has pThis->doSomething(); etc all through it as if it were an attempt at OOP using C.
The problem is that a static function is not inside the space of the class, so to speak. It does not have a valid this pointer, you might say. Thus the problem is that you cannot access non-static data inside a static class function.
The solution, as everyone says, would be to take a pointer to the actual class instance and fetch the variables from it or call a non-static member function on it.
There is a simple way to do this. Your static function does nothing but call a non-static function to do whatever needs to be done. The non static function can then access whatever it needs to and returns to the static function. The static function returns to caller and everyone is happy.
If you have a great number of non-statics you must access this is the route I would choose.
Essentially:
Which has already been said.Quote:
Yes and the actual thread function that receives this pointer should simply be a tiny stub function that calls a member function on that pointer to do the real work. That way you don't end up with a function that has pThis->doSomething(); etc all through it as if it were an attempt at OOP using C.