Christopher Pisz <
nos...@notanaddress.com> wrote:
> Can someone break this cast down for me?
> Example: (int (*)(int))std::toupper
If you have a defintion like this
int (*foo)(double);
'foo' is a pointer variable to a function that takes an double
as its (only) argument and returns an int.
And, in that spirit,
(int (*)(int)) std::toupper
casts the address of the std::toupper function to a pointer
to a function that takes an int argument and returns an int.
You can also use this with typedefs, e.g.
typedef void (*sighandler_t)(int);
makes 'sighandler_t a type that is pointer to a function that
takes an int argument and returns void. This is used in the
declaration of the UNIX signal() function which sets up a
handler function (taking an int and returning void) for the
signal numbered 'signo' (and returns the previous handler):
sighandler_t signal(int signum, sighandler_t handler);
which would be a handful to parse without the typedef:
void (signal(int, void (*)(int)))(int, void (*)(int));
(I hope I got that right;-)
> What are each of the parts in the brackets?
> I am see C-style cast (int)myvariable, but never
> (type(otherstuff)(morestuff))function
That's probably not what your'e going to see (at least I don't
know what it would be supposed to mean;-), it's more more like
(rettype (*)(argtype1, argtype2)) function
which casts the address of 'function' to a pointer to a function
which returns 'rettype' and accepts two arguments, the first one
of type 'argtype1' and the second of type 'argtype2'.
Regards, Jens
--
\ Jens Thoms Toerring ___
j...@toerring.de
\__________________________
http://toerring.de