472,802 Members | 1,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

typecasting function pointer to void*

How to typecast a "function pointer" to "const void*" type in C++ way?

int MyFunction (double money); // Function prototype

const void* arg = (const void*)MyFunction; // type casting function
pointer to const void* in C-style

void(*pFunc)() = (void(*)())(arg); // type casting const void* to function
pointer in C-style
(*pFunc)(); // Calling the function after type casting is done

Is typecasting like above is safe anyway?

regards,
Sukumar
Jul 27 '06 #1
5 11147

WittyGuy schrieb:
How to typecast a "function pointer" to "const void*" type in C++ way?

int MyFunction (double money); // Function prototype
I suggest you to create a typedef for the function pointer:
typedef int (*TMyFunction) ( double );

And then use it!
void * ptr = static_cast<void*>(&some_func);
....
(static_cast<TMyFunction>(ptr))(23.42):

Jul 27 '06 #2
WittyGuy wrote:
How to typecast a "function pointer" to "const void*" type in C++ way?
There is no "how".
int MyFunction (double money); // Function prototype

const void* arg = (const void*)MyFunction; // type casting function
pointer to const void* in C-style

void(*pFunc)() = (void(*)())(arg); // type casting const void* to function
pointer in C-style
(*pFunc)(); // Calling the function after type casting is done
You are casting back to a different signature. Even if there was a roundtrip
guarantee through void* (which there is not), this conversion would be
unspecified.
Is typecasting like above is safe anyway?
No: In C++ there is no round-trip guarantee for pointer-to-function or
pointer-to-member-function to void* and back. Just don't do it.

Function pointers are convertible to a different signature, however, the
result of such conversion cannot be used: it can only be converted back.
However, this does allow you to use void(*)(void) as a universal function
pointer to and from which you can cast (reinterpret_cast is the one you
might want to use) [5.2.10/6].
Best

Kai-Uwe Bux
Jul 27 '06 #3
WittyGuy wrote:
How to typecast a "function pointer" to "const void*" type in C++ way?
Not possible in standard C++. You can only cast object pointers to void*,
not function pointers.
int MyFunction (double money); // Function prototype

const void* arg = (const void*)MyFunction; // type casting function
pointer to const void* in C-style

void(*pFunc)() = (void(*)())(arg); // type casting const void* to function
pointer in C-style
(*pFunc)(); // Calling the function after type casting is done

Is typecasting like above is safe anyway?
It is not safe at all. Assuming that your compiler supports the conversion
function pointer -void pointer as an extension (which is not too
uncommon), it would still be very dangerous, because you're calling a
function that takes one argument through a pointer to function that takes
no arguments.

Jul 27 '06 #4
I think you can use reinterpret cast.
Rolf Magnus wrote:
WittyGuy wrote:
How to typecast a "function pointer" to "const void*" type in C++ way?

Not possible in standard C++. You can only cast object pointers to void*,
not function pointers.
int MyFunction (double money); // Function prototype

const void* arg = (const void*)MyFunction; // type casting function
pointer to const void* in C-style

void(*pFunc)() = (void(*)())(arg); // type casting const void* to function
pointer in C-style
(*pFunc)(); // Calling the function after type casting is done

Is typecasting like above is safe anyway?

It is not safe at all. Assuming that your compiler supports the conversion
function pointer -void pointer as an extension (which is not too
uncommon), it would still be very dangerous, because you're calling a
function that takes one argument through a pointer to function that takes
no arguments.
Jul 27 '06 #5
ms******@gmail.com wrote:
I think you can use reinterpret cast.
Don't top-post.
You can get the compiler to shut up about the illegal conversion, but
that doesn't mean it will work.


Brian
Jul 27 '06 #6

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

Similar topics

3
by: Kapil Khosla | last post by:
Hi, I have been trying to understand this concept for quite sometime now somehow I am missing some vital point. I am new to Object Oriented Programming so maybe thats the reason. I want to...
3
by: Robert Street | last post by:
Hi! I'm rather new at c++ and I'm totally confused with this kind of typecasting: typedef signed char int8_t; typedef signed short int16_t; typedef struct broadcast_hdr {
11
by: Vinod Patel | last post by:
I have a piece of code : - void *data; ...... /* data initialized */ ...... struct known_struct *var = (struct known_struct*) data; /*typecasting*/ How is this different from simple...
3
by: bnoordhuis | last post by:
Consider this: int foo(int *a, int *b); int (*bar)(void *, void *) = (void *)foo; How legal - or illegal - is the typecast and are there real-world situations where such code will cause...
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
12
by: srinivas.satish | last post by:
Hi, is it possible to typecast a function pointer to two different prototypes. eg., typedef void (functptr1 *) (int , int); typedef void (functptr2 *) (int); functptr1 fptr; fptr =...
1
by: ramakanta.sinha | last post by:
Hi, While typecasting an integer to (void *) the following warning is found. warning: cast to pointer from integer of different size. This is the line where typecasting is done. fld->base...
12
by: bwaichu | last post by:
What is the best way to handle this warning: warning: cast from pointer to integer of different size I am casting in and out of a function that requires a pointer type. I am casting an...
3
by: sritejv | last post by:
Hello everyone, I am having a problem with typecasting void pointers.I have read the pointer basics but still cant understand why the following test code doesnt work. void *xyz; struct abcd...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.