In the following C++ code what does double (*) double
mean?
What kind of a return type is it?
auto get_fun(int arg) -> double (*)(double) // same as: double (*get_fun(int))(double)
{
switch (arg)
{
case 1: return std::fabs;
case 2: return std::sin;
default: return std::cos;
}
}
double (*)(double)
it's a function pointer signature for a function that takes one double
argument and returns double
. Generally
X (*)(A, B, C) // any number of args
is a pointer to function that takes args of types(A, B, C)
and returns value of type X
, e.g.
X my_func(A, B, C) {
return X(); // assuming this makes sense
}
would be of the signature above.
So in your case get_fun
is a function that returns a function pointer.
double (*)(double)
is type representing a pointer on function taking double
and returning double
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With