I have made this "model" (I was unable to test it, problems with SDL
). What do you think of it?
Code:
//header.h
class Base{
public:
/* "p" is the data that the thread will run on,
the parameter can be stored in class, and never
be used as a paramater to the thread ;) */
Base(void* p);
/* "callback" function of the thread, receives a base class */
static int call_update(void*);
/* thread function itself */
virtual int update(void*)=0;
void join(){ SDL_WaitThread(thread, NULL); }
private:
/* pointer that will hold the data */
void* data;
SDL_Thread* thread;
};
class Creature : public Base{
public:
Creature();
int update(void*);
private:
// who cares...
};
class Mage : public Base{
public:
Mage(int power);
int update(void*);
private:
int myPower;
};
Code:
//implementation.cpp
Base::Base(void* p)
{
data = p;
thread = SDL_CreateThread(Base::call_update, this);
}
int Base::call_update(void* base_class)
{
Base* b = static_cast<Base*>(base_class);
b->update(b->data);
}
/* We don't use any parameter in Creature,
so the pointer can be NULL */
Creature::Creature()
: Base(NULL){}
int Creature::update(void*)
{
for(int i=0;i<3;i++)
cout << "I am a Creature counting... " << i << endl;
}
Mage::Mage(int power)
: Base(&myPower){
myPower = power;
}
int Mage::update(void*)
{
cout << "I am a mage with power: " << myPower << endl;
}