I have a class called Thread derived from wxThread.
Every derived class from wxThread needs a Entry() function where the execution of this thread starts.
I did not want to make several classes that differ only in the entry()-function (which all called different memberfunctions of my "main"class.).
Thats why i put a function pointer as parameter in the constructor of the Thread-class.
here is the code i am going to talk about:
Code: Select all
//thread.h
#include <wx/thread.h>
class Thread : public wxThread
{
private:
void (Zentrale::*adress)(); // pointer to be accessed by Entry
public:
Thread(void (Zentrale::*functiontocall)());
~Thread();
virtual void* Entry();
};
Code: Select all
//thread.cpp
#include <zentrale.h> //definnition of the class Zentrale
#include <thread.h>
DECLARE_APP(Zentrale) //needed for wxGetApp()
//this class is a friend of the Zentrale-class
Thread::Thread(void (Zentrale::*functiontocall)())
{
adress = functiontocall; //copy adress of passed function to temporary pointer
}
Thread::~Thread() { }
void *Thread::Entry()
{
(wxGetApp().*adress)();
}
Code: Select all
//...
//in a memberfunction of Zentrale
Thread *thread = new Thread(&Zentrale::recv_thread);
Thread *thread2 = new Thread(&Zentrale::call_thread);
if (thread->Create() == wxTHREAD_NO_ERROR) { thread->Run(); }
if (thread2->Create() == wxTHREAD_NO_ERROR) { thread2->Run(); }
//...
the first thing is, i pass the adress of the function in Zentrale to the constructor and there this adress has to be copied in a temporary pointer (of course of the same type) that the adress can be used in Entry to access the function.
how can i avoid this second pointer?
the second thing is:
how can i make this Thread class to not say by default that the passed pointer is a pointer to a memberfunction of the Zentrale class, but a pointer to a user-defined class.
The Thread class should accept any class, how can i make that? if you know what i mean