473,837 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*)MyFunctio n; // 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 11251

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<voi d*>(&some_func) ;
....
(static_cast<TM yFunction>(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*)MyFunctio n; // 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_ca st 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*)MyFunctio n; // 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*)MyFunctio n; // 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
8193
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 understand what is typecasting in C++. Say I have a Base class and a Derived class, I have a pointer to an object to each. Base *ba = new Base; Derived *de = new Derived;
3
5588
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
4442
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 assignment. int b = some_value;
3
3995
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 trouble? I don't mean trouble like in 'not compiling' but trouble like in 'crashing hard'.
16
10411
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 do you mean by pointing to a void and why is it done? Aso, what happens when we typecast a pointer to another type. say for example int *i=(char *)p; under different situations? I am kind of confused..can anybody clear this confusion by clearly...
12
8400
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 = somefunction_name; fptr(10,20); fptr = (functptr2)someotherfunction_name;
1
1558
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 = (void *)(((fld->f90_table_index-1) * F90_MAX_FLD_SIZE_IN_BYT
12
4491
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 integer as a pointer, but the pointer is 8 bytes while the integer is only 4 bytes. Here's an example function:
3
13156
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 { int a; };
0
9682
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10566
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10271
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9398
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7806
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7000
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5668
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4040
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3124
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.