+ 7

Why the output is 1?

#include <iostream> using namespace std; int s (int a){ return 165;} int main() { int(*e)(int)=s; cout << *e ; return 0; }

24th May 2019, 10:10 AM
//(AnOnYmOuS);
1 Resposta
+ 2
Because ostream doesn't have an overload for the type 'int(*)(int)'. It would be impossible to define them all because there are an infinite amount of function pointer types. The next best option is to convert it to a boolean and since functions can't be located at address 0 they are all evaluated as 1. You can print the address by overloading int(*)(int) for ostream operator<< yourself. std::ostream& operator<<( std::ostream& os, int(*p)(int) ) { return os << (void*)p; } What you probably want is cout << e(5); // 165
24th May 2019, 10:36 AM
Dennis
Dennis - avatar