473,386 Members | 1,997 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.

Using function pointers in c++

Hi!

I have had this problem several times before, and each time I have
been turned back forced to find another way. But not this time...

How can you cast a Class::* into a void *.

For this example I am using pthread and to start a thread you need to
pass it a void* (*)(void*)

however I have the run function inside a class:

class ThreadBase {
public:
ThreadBase() {}
virtual void run() = 0;
void start_thread() { pthread_create( &__thread_id , 0 ,this-
>run , 0 ); }
void join_thread();
private:
pthread_t __thread_id;
};

Now if you inherit from this class and make a run function and call
start_thread you recieve this error:

error: cannot convert void (ThreadBase::*)() to void* (*)(void*)

This doesn't only happen with classes, if you put a function in a
namespace I believe you get a similar error.

Is there a solution, or must you solve the problem another way?

Thanks allot!

Matt

Feb 21 '07 #1
5 1757
Ma*************@gmail.com wrote:
Hi!

I have had this problem several times before, and each time I have
been turned back forced to find another way. But not this time...

How can you cast a Class::* into a void *.

For this example I am using pthread and to start a thread you need to
pass it a void* (*)(void*)

however I have the run function inside a class:

class ThreadBase {
public:
ThreadBase() {}
virtual void run() = 0;
void start_thread() { pthread_create( &__thread_id , 0 ,this-
>>run , 0 ); }

void join_thread();
private:
pthread_t __thread_id;
};

Now if you inherit from this class and make a run function and call
start_thread you recieve this error:

error: cannot convert void (ThreadBase::*)() to void* (*)(void*)

This doesn't only happen with classes, if you put a function in a
namespace I believe you get a similar error.

Is there a solution, or must you solve the problem another way?

Thanks allot!

Matt
Of course there is a way.

Your must pass a function of the correct type to pthread_create but
there is no reason that function cannot call ThreadBase::run

Like this

void run_my_thread(void* my_this)
{
((ThreadBase*)my_this)->run();
}

void ThreadBase::start_thread()
{
pthread_create( &__thread_id , 0 , run_my_thread , this );
}

This is a FAQ of course,

http://www.parashift.com/c++-faq-lit....html#faq-33.2

john
Feb 21 '07 #2
Ma*************@gmail.com wrote:
Hi!

I have had this problem several times before, and each time I have
been turned back forced to find another way. But not this time...

How can you cast a Class::* into a void *.

For this example I am using pthread and to start a thread you need to
pass it a void* (*)(void*)

however I have the run function inside a class:

class ThreadBase {
public:
ThreadBase() {}
virtual void run() = 0;
void start_thread() { pthread_create( &__thread_id , 0 ,this-
>run , 0 ); }
void join_thread();
private:
pthread_t __thread_id;
};

Now if you inherit from this class and make a run function and call
start_thread you recieve this error:

error: cannot convert void (ThreadBase::*)() to void* (*)(void*)

This doesn't only happen with classes, if you put a function in a
namespace I believe you get a similar error.

Is there a solution, or must you solve the problem another way?
In short, you can't, nor do you want to.

http://www.parashift.com/c++-faq-lit....html#faq-33.2
Feb 21 '07 #3
John Harrison wrote:
>
Like this

void run_my_thread(void* my_this)
{
((ThreadBase*)my_this)->run();
}
Better is

extern "C" void run_my_thread(void* my_this)
{
static_cast<ThreadBase*>(my_this)->run();
}

In general, most C style callback interfaces generally require C linkage.
>
void ThreadBase::start_thread()
{
pthread_create( &__thread_id , 0 , run_my_thread , this );
}

Feb 21 '07 #4
Ma*************@gmail.com wrote:
Hi!

I have had this problem several times before, and each time I have
been turned back forced to find another way. But not this time...

How can you cast a Class::* into a void *.

For this example I am using pthread and to start a thread you need to
pass it a void* (*)(void*)

however I have the run function inside a class:

class ThreadBase {
public:
ThreadBase() {}
virtual void run() = 0;
void start_thread(){pthread_create( &__thread_id, 0, this->run, 0 );}
You just can't do this. The pthread_create function expects a C
function of the form void* (*)(void*). Any class member function will
have a hidden this parameter, which isn't a void*. A class member
function has C++ linkage.

The only correct form is to declare a function extern "C" and pass that
function to pthread_create.

extern "C" void start( void* );

void start( void *p )
{
ThreadBase *base = static_cast<ThreadBase*>(p);
base->run();
}

--
Ian Collins.
Feb 21 '07 #5
extern "C" void start( void* );
>
void start( void *p )
{
ThreadBase *base = static_cast<ThreadBase*>(p);
base->run();

}

--
Ian Collins.
Thank you all very much! Worked like a charm.

Matt

Feb 21 '07 #6

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

Similar topics

40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
12
by: Christof Krueger | last post by:
Hello, I'm quite new to C++ so maybe there's something I miss. I write a simple board game. It has a board class. This class has a method that returns the count of pieces a player has on the...
2
by: V. de Bellabre | last post by:
Hello all, I'm currently working on a 3D graphic engine but I'm stuck with a small problem on references of pointers. Some of my c++ class are complex and use many pointers so I decided to force...
10
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
57
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
15
by: arnuld | last post by:
-------- PROGRAMME ----------- /* Stroustrup, 5.6 Structures STATEMENT: this programmes *tries* to do do this in 3 parts: 1.) it creates a "struct", named "jd", of type "address". 2. it...
6
by: =?utf-8?B?4piG4piG4piG4piG4piGIFPDvCBLZWl0aCBDaGFr | last post by:
Ok. Let's say Im writing a game and I have a class table for my character's stats. (Don't worry about classes yet ,you'll run into them soon enough). Say this table includes all his stats, age,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.