cast question

P

Patrick Guio

Dear all,


I have a C function that returns a void pointer (void *).
This object pointer is actually a pointer to function.
I have the following declarations:

void *pobj;
double (*func)(double);

I am trying to convert pobj into func with a reinterpret_cast

fun = reinterpret_cast<double (*)(double)>(pobj);

but I get a compiler error that says that ISO C++ forbids casting between
pointer-to-function and pointer-to-object.

Any idea how to force such cast ?

Sincerely,

Patrick
 
I

Ioannis Vranos

Patrick said:
I have a C function that returns a void pointer (void *).
This object pointer is actually a pointer to function.
I have the following declarations:

void *pobj;
double (*func)(double);

I am trying to convert pobj into func with a reinterpret_cast

fun = reinterpret_cast<double (*)(double)>(pobj);

but I get a compiler error that says that ISO C++ forbids casting
between pointer-to-function and pointer-to-object.

Any idea how to force such cast ?


By using a C style cast:


func=(double (*) (double) ) (pobj);
 
S

Siemel Naran

Patrick Guio said:
I am trying to convert pobj into func with a reinterpret_cast

fun = reinterpret_cast<double (*)(double)>(pobj);

but I get a compiler error that says that ISO C++ forbids casting between
pointer-to-function and pointer-to-object.

My compiler (Borland 6) allows the cast, though another (g++ 2.95) forbids
it. I guess g++ is right.

However, you can reinterpret_cast from one function type to another, which
might be be of use.
 
S

Serock

You can convert pobj into func as below:

void *pobj;
double (*func)(double);
*((void**)&func) = pobj;

under g++ 2.4.2
 
S

Serock

You can try to convert pobj into func as below:

void *pobj;
double (*func)(double);
*((void**)&func) = pobj;

can pass under g++ 3.4.2
 
J

Jack Klein

Dear all,


I have a C function that returns a void pointer (void *).
This object pointer is actually a pointer to function.
I have the following declarations:

void *pobj;
double (*func)(double);

I am trying to convert pobj into func with a reinterpret_cast

fun = reinterpret_cast<double (*)(double)>(pobj);

but I get a compiler error that says that ISO C++ forbids casting between
pointer-to-function and pointer-to-object.

Any idea how to force such cast ?

Sincerely,

Patrick

There is no defined conversion between any pointer to object type,
including pointer to void, and any pointer to function type. A
C-style cast is accepted, but the result is completely undefined.

Complain to the source of the function, it is, has been, and always
will be bad code.
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top