473,407 Members | 2,676 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,407 software developers and data experts.

cast question


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
Jul 22 '05 #1
5 2005
Patrick Guio wrote:
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);


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #2
"Patrick Guio" <pa******@ii.uib.no> wrote in message
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.
Jul 22 '05 #3
You can convert pobj into func as below:

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

under g++ 2.4.2

Jul 22 '05 #4
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

Jul 22 '05 #5
On Mon, 6 Dec 2004 10:31:20 +0100, Patrick Guio <pa******@ii.uib.no>
wrote in comp.lang.c++:

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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #6

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

Similar topics

4
by: Richard Lee | last post by:
Hi, I have a question when I do a data type cast. the common way when we do a cast, is we know the type we want to cast to, i.e. we want to cast object to string, object xyz = "question";...
15
by: Christopher Benson-Manica | last post by:
If you had an unsigned int that needed to be cast to a const myClass*, would you use const myClass* a=reinterpret_cast<const myClass*>(my_val); or const myClass* a=(const myClass*)myVal; ...
5
by: MC | last post by:
Hi If I have a pointer to a some structure say for example payroll_ptr where struct payroll { ... } has some members and if i use a function argument as int function_process ( (payroll_ptr)...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
1
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a...
5
by: Frederick Gotham | last post by:
Before I begin, here's a list of assumptions for this particular example: (1) unsigned int has no padding bits, and therefore no invalid bit- patterns or trap representations. (2) All types have...
4
by: dba_222 | last post by:
Dear Experts, Ok, I hate to ask such a seemingly dumb question, but I've already spent far too much time on this. More that I would care to admit. In Sql server, how do I simply change a...
4
by: Pavel Minaev | last post by:
There was an earlier discussion regarding which one is faster - a throwing cast - "(Foo)obj", or a non-throwing one - "obj as Foo", when both are available for a given type and value. The consensus...
2
by: =?Utf-8?B?Unlhbg==?= | last post by:
Hi, How can I get around runtime error that says I can not explicit cast List<SubClassto ICollection<Class>? Generic List inhertis generic ICollection and Subclass inherits Class, then...
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?
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.