L
Luke Dalessandro
Code: Thread -> U
-> T
public class Thread {
protected:
thread_t _tid;
virtual void foo() = 0;
public:
// Static entry function for the internal thread
static void* ThreadEntry(void* pThread) {
Thread* thread = static_cast<Thread*>(pThread);
// ***
// *** This is the error, calls the pure virtual version...?
// ***
thread->foo();
return 0;
}
// Starts the internal thread running
void Start() {
pthread_create(&_tid, NULL, Thread::ThreadEntry, this);
}
}
public class T : public Thread {
protected:
virtual void foo() {
cout << "I'm a T";
}
}
public class U : public Thread {
protected:
virtual void foo() {
cout << "I'm a U";
}
}
As you can see I'm just trying to re-use some pthread code for a
multithreaded server that spawns new threads for different reasons. I
can't figure out how to cast the void* to a base Thread* pointer and
call the pure virtual "foo" function and get the derived behavior...
Of course, the standard usage like
Thread* u = new U();
u->foo();
calls the derived foo like I expect.
I also tried a non-pure virtual foo and the Thread::foo is still getting
called after the cast.
I'm on OSX 10.4 development version. Not sure which gcc it is.
Thanks in advance,
Luke
-> T
public class Thread {
protected:
thread_t _tid;
virtual void foo() = 0;
public:
// Static entry function for the internal thread
static void* ThreadEntry(void* pThread) {
Thread* thread = static_cast<Thread*>(pThread);
// ***
// *** This is the error, calls the pure virtual version...?
// ***
thread->foo();
return 0;
}
// Starts the internal thread running
void Start() {
pthread_create(&_tid, NULL, Thread::ThreadEntry, this);
}
}
public class T : public Thread {
protected:
virtual void foo() {
cout << "I'm a T";
}
}
public class U : public Thread {
protected:
virtual void foo() {
cout << "I'm a U";
}
}
As you can see I'm just trying to re-use some pthread code for a
multithreaded server that spawns new threads for different reasons. I
can't figure out how to cast the void* to a base Thread* pointer and
call the pure virtual "foo" function and get the derived behavior...
Of course, the standard usage like
Thread* u = new U();
u->foo();
calls the derived foo like I expect.
I also tried a non-pure virtual foo and the Thread::foo is still getting
called after the cast.
I'm on OSX 10.4 development version. Not sure which gcc it is.
Thanks in advance,
Luke