+ 1

can someone explain this ?

int sum(int a=2,int b=9){ return a+b; } int main() { int(*psum)(int,int); psum=&sum; cout<<*psum; //output1 return 0; }

11th Apr 2019, 6:43 AM
San Anemos
San Anemos - avatar
2 Respuestas
+ 6
You have a function pointer that points to a function which takes 2 ints and returns one, you assign sum to it and print the function without calling it, so you get true/1.
11th Apr 2019, 8:04 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 1
Also the reason why it prints 1 is because cout doesn't have an overload for a int(*)(int,int) function pointer. The next available option is a bool which just converts any address to a 1. You'd need to overload the specific function pointer yourself for std::ostream operator<< if you want it to print it's actual address.
11th Apr 2019, 8:21 AM
Dennis
Dennis - avatar