Cannot covert non-static member function to normal function?

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.
 
V

Victor Bazarov

Morgan Cheng said:
[..]
I am wondering why C++ doesn't allow non-static member function to be
coverted into normal function.

Because they require an instance of the class in order to be called.
Imagine that such conversion would be allowed, where would it get the
object whose address will be in 'this' pointer?

V
 
D

David Harmon

On Fri, 17 Dec 2004 10:02:53 +0800 in comp.lang.c++, Morgan Cheng
I am wondering why C++ doesn't allow non-static member function to be
coverted into normal function.

A non-static member function must be called in such a way that it is
passed a 'this' pointer. Calling a normal function cannot do that.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[33.2] How do I pass a pointer-to-member-function to a signal
handler, X event callback, system call that starts a thread/task,
etc?" It is always good to check the FAQ before posting. You can
get the FAQ at:
http://www.parashift.com/c++-faq-lite/
 
J

Jonathan Mcdougall

Morgan said:
I am wondering why C++ doesn't allow non-static member function to be
coverted into normal function.

Because you need a this pointer, that is, you need an object to call the
member function on.


Jonathan
 
M

Morgan Cheng

Thanks for you clarification.
But I think that, from semantic perspective, the reason is that each
non-static member function has an implicit parameter of pointer to
"this". As a result,
If "int foo(int data);" is declared as a member function, its signature
is actually different from normal function because it has one more
parameter.



David said:
On Fri, 17 Dec 2004 10:02:53 +0800 in comp.lang.c++, Morgan Cheng
I am wondering why C++ doesn't allow non-static member function to be
coverted into normal function.


A non-static member function must be called in such a way that it is
passed a 'this' pointer. Calling a normal function cannot do that.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[33.2] How do I pass a pointer-to-member-function to a signal
handler, X event callback, system call that starts a thread/task,
etc?" It is always good to check the FAQ before posting. You can
get the FAQ at:
http://www.parashift.com/c++-faq-lite/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top