/*
Here is the situation:
You have a class with a non static function.
You have instances of that class.
The function of the class needs to use the 'this' keyword to access
the values of those instances.
You want to call this function as a new thread.
You get the error "'_beginthreadex' : cannot convert parameter 3 from 'int' to
'unsigned int (__stdcall *)(void *)'"
or something like "error C3867: '[classname]::[functionname]': function call
missing argument list; use '&[classname]::[functionname]' to create a pointer to member"
Here is the workaround solution:
1 You must make the member function a static member function of type
'static unsigned __stdcall [your_function_name](void *)'
2 When you create the function thread you will pass it a pointer to the
instance you want to start the thread in, casted to void * so the
function is always the same (static) but the pointer to the information
it uses is different (which is the instance of your class).
3 Inside the function you cast the void * back to a class pointer then
put it in a Fakethispointer variable and use it as a this pointer.
Code example:
*/
class Bunch_Of_Chores
{
public:
static unsigned __stdcall Do_The_Dishes(void *);
string Stupid_Chores;
bool Throw_Dishes_Away;
bool Buy_Disposable;
};
...
Bunch_Of_Chores c;
//instead of passing arguments directly we can just put them inside the instance of the class
c.Stupid_Chores = "Gross dishes that have been in the sink forever and have mold";
//pass the address of where the instance is (class c) as the paramater
//_beginthreadex requires that it be casted to void
_beginthreadex(NULL,0,c.Do_The_Dishes,(void*)&c,0,NULL);
...
unsigned __stdcall Bunch_Of_Chores::Do_The_Dishes(void * p)
{
Bunch_Of_Chores * Fake_This_Pointer;
Fake_This_Pointer = (Bunch_Of_Chores*)p;
MessageBox(NULL,Fake_This_Pointer->Stupid_Chores.c_str(),"",MB_OK);
Fake_This_Pointer->Throw_Dishes_Away = true;
Fake_This_Pointer->Buy_Disposable = true;
return 0;
}