Pointer to member function
I want to implement a C++ wrapper to C code that requires a function
pointer to be passed in. Specifically, I want a wrapper for pthread that
clients can use as a base class.
Is there a way of passing a non-static member function in that will do
this?
In the following code, if I use the static k_main then it builds and
runs, but the derived class uses the implementation in CMyThread.
If I use a virtual member function, I get a compile error:
thread2.cpp:21: error: argument of type ?void* (CMyThread::)(void*)?
does not match ?void* (*)(void*)?
Presumably this is because the member function pointer still expect the
this parameter when it is called.
I have thought of an alternative way that requires the CMyThread
constructor to pass the this pointer as an additional argument that is
then used in k_main to call the member function. It just looks really
horrible.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
class CMyThread
{
protected:
pthread_t m_thread;
void *m_args;
static void *k_main( void *ptr );
virtual void *m_main( void *ptr );
public:
CMyThread(char *name);
virtual ~CMyThread();
};
CMyThread::CMyThread(char *args)
: m_args(args)
{
int rc = pthread_create( &m_thread, NULL, m_main, (void *)args);
}
CMyThread::~CMyThread()
{
pthread_join( m_thread, NULL);
}
void *CMyThread::k_main(void *args)
{
char *message;
message = (char *) args;
printf("Base Static %s \n", message);
}
void *CMyThread::m_main(void *args)
{
char *message;
message = (char *) args;
printf("Base Virtual %s \n", message);
}
class CMyDerivedThread : public CMyThread
{
protected:
static void *k_main( void *ptr );
virtual void *m_main( void *ptr );
public:
CMyDerivedThread(char *name);
virtual ~CMyDerivedThread();
};
CMyDerivedThread::CMyDerivedThread(char *args)
: CMyThread(args)
{
}
CMyDerivedThread::~CMyDerivedThread()
{
}
void *CMyDerivedThread::k_main(void *args)
{
char *message;
message = (char *) args;
printf("Derived Static %s \n", message);
}
void *CMyDerivedThread::m_main(void *args)
{
char *message;
message = (char *) args;
printf("Derived Virtual %s \n", message);
}
main()
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
CMyThread t1(message1);
CMyThread t2(message2);
CMyDerivedThread("Thread 3");
exit(0);
}