I don’t know if this this even possible but I have an idea of creating an abstract class to make threads using WinAPI. This class would be inheritaded by class which will define the function to execute in the thread.
I have two classes:
class XYZ{
//...
class ABC: public XYZ{
//...
And i have a construct
ABC::ABC()
{
addThread(this,1);
}
addThread is defined inside XYZ class:
void addThread(void* self, int a)
{
DWORD ThreadID;
THREAD_DATA *threadData;
threadData->self = self;
threadData->thread_id = a;
CreateThread(NULL, 0, createThread, threadData, 0, &ThreadID);
}
As you can see i am creating a structure which contains two fields: object of the superclasses and the number of thread (so the method can know which data use).
This is how the structure looks like:
typedef struct _THREAD_DATA {
void* self;
int thread_id;
} THREAD_DATA;
The createThread in an static method of XYZ class:
static DWORD WINAPI createThread(void* _threadData)
{
THREAD_DATA* threadData = (THREAD_DATA*) _threadData;
return threadData->self->execute(threadData->thread_id);
}
And here is the place where everything goes wrong.
My VSC++ 2010 is underlining the first word “threadDataself” in the second line of the static method body. After compilation I get an error:
left of ‘->execute’ must point to class/struct/union/generic type
I have no idea how to fix it. I wrote this code using those links:
http://goo.gl/7VYC3 & http://goo.gl/ETfe7
The execute method is decalared in the XYZ class as:
virtual DWORD execute(int i);
and then defined in the ABC class as
DWORD ABC::execute(int i){
//...
Can anyone please help me? I would appreciate any tips how to make this works.
“self” is defined as a void pointer, and you are trying to call the “execute” method on it, which does not exist. you need to change the type of self to the base class that contains the method “execute.”
Like this:
When attempting to call an overridden method polymorphically as you are here, you must make sure the type of the object you are calling the method on is the base class type. You cannot use a void pointer.