M
Morgan Cheng
I tried to build a CThread on linux which imitate the behavior of java
Thread. I think this is still in the scope of C++.
First, I tried this way
#include <pthread.h>
class CThread
{
public:
void start();
void run();
private:
void * threadProc(void *data);
pthread_t m_tid;
};
void CThread::start()
{
pthread_create(&m_tid, NULL, this->threadProc, NULL)
}
void CThread::run()
{
//do nothing
}
void * CThread::threadProc(void *data)
{
run();
}
User shall create a subclss of CThread and override the function run().
But when I compile it, the compiler says:
cannot convert `void*(CThread::*)(void*)' to `void*(*)(void*)'
for argument `3' to `int pthread_create(pthread_t*, const
pthread_attr_t*, void*(*)(void*), void*)'
So I change threadProc into a static function. Then it works.
I am wondering why C++ doesn't allow non-static member function to be
coverted into normal function.
Thread. I think this is still in the scope of C++.
First, I tried this way
#include <pthread.h>
class CThread
{
public:
void start();
void run();
private:
void * threadProc(void *data);
pthread_t m_tid;
};
void CThread::start()
{
pthread_create(&m_tid, NULL, this->threadProc, NULL)
}
void CThread::run()
{
//do nothing
}
void * CThread::threadProc(void *data)
{
run();
}
User shall create a subclss of CThread and override the function run().
But when I compile it, the compiler says:
cannot convert `void*(CThread::*)(void*)' to `void*(*)(void*)'
for argument `3' to `int pthread_create(pthread_t*, const
pthread_attr_t*, void*(*)(void*), void*)'
So I change threadProc into a static function. Then it works.
I am wondering why C++ doesn't allow non-static member function to be
coverted into normal function.