There are more kinds of cast styles in C++:
http://www.cplusplus.com/doc/tutorial/typecasting/
The traditional style cast, inherited from C:
(double)i;
The "functional" style cast, which mimics object constructors:
double(i);
And the new style casts, which use a keyword and are more specialized:
static_cast<double> (i); // there are others beside static_cast, read the link above
Basically what happens in your code is that you use the functional style cast, until you put parentheses around it making it a C style cast.
And then
double(int)
is the signature of a function returning a
double and having an
int parameter. You can't directly use
double(int)
as a type however (hopefully someone more knowledgeable than I can explain why not).
Below is an example demonstrating function pointers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
double f(int i)
{
return i*i;
}
int main()
{
typedef double(my_function)(int);
my_function *pf = &f; // pointer to function
std::cout << (*pf)(2) << std::endl;
}
|
4 |
Edit: and here's the C++11 version of the above example, which doesn't use
typedef:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
double f(int i)
{
return i*i;
}
int main()
{
using my_function = double(int);
my_function *pf = &f; // pointer to function
std::cout << (*pf)(2) << std::endl;
}
|
4 |
You will need a modern compiler that supports the C++11 standard to compile the above. I'm using MinGW GCC 4.8.1, and Visual Studio 2012 should work as well.