473,386 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Cannot covert non-static member function to normal function?

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.
Jul 22 '05 #1
4 2346
"Morgan Cheng" <mo************@gmail.com> wrote...
[..]
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
Jul 22 '05 #2
On Fri, 17 Dec 2004 10:02:53 +0800 in comp.lang.c++, Morgan Cheng
<mo************@gmail.com> wrote,
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/

Jul 22 '05 #3
Morgan Cheng wrote:
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
Jul 22 '05 #4
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 Harmon wrote:
On Fri, 17 Dec 2004 10:02:53 +0800 in comp.lang.c++, Morgan Cheng
<mo************@gmail.com> wrote,
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/

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Philip Mette | last post by:
I am in a crunch and need to covert this Oracle statement to MSSQL. Is there any Oracle/MSSQL experts out there that can help me? I do not understand the syntax enough to modify this. Thanks so...
3
by: Cindy Liu | last post by:
Hi Everyone, In .Net we have XmlSerializer to convert c# class to xml, or vice versa. Do we have any APIs to convert c# class to XSD? Thanks in advance, Cindy
0
by: ad | last post by:
I use ado.net to fill a Excel wroksheet into a DataTable. The data in the Excel wroksheet is digital. After the data filled into the DataTable, the DataType of each column is set to Double, but...
9
by: Andy Sutorius | last post by:
Hi, I am receiving the error when compiling the project, "cannot implicitly convert type object to string". The error points to this line of code and underlines the dtrRecipient:...
3
by: ad | last post by:
I use a fileUpload of ASP.NET to read a csv file from client into a Strem like: Stream myStream = FileUpload1.FileContent; But the file from Client if of CharSet BIG5. How can I covert it to...
6
by: Calros Lo | last post by:
Dear all: I develop a programe that need when I get a string , such as "123" or "ABC",if I get string "123" and the system will help me to create new string "124" , if I get string "ABC" and the...
21
by: Wisdo | last post by:
Hi All, char to char ** is compile error. why? if i hava a string array. yes. it's not safe and it's better to use vector<stringinstead. but my point is the language feature. char...
6
by: =?Utf-8?B?dmluY2VudA==?= | last post by:
Is there any method to covert a pdf file into jpg file in c#? Many thanks for your replying.
2
boss32178
by: boss32178 | last post by:
Ok, I am new to the form and been working with c# for 3 months now. I am creating a dll that i need to use in my asp.net page The dll grabs all the forms and creat a xml page, and grabs a xml page...
2
boss32178
by: boss32178 | last post by:
Ok I get this error when i try to run the web app. foreach statement cannot operte on variables of type 'object' does not contain a public definition for 'GetEnumerator' here is the code #region...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.