Actually, what cout << b; does is write out the address of the function b()
No: the name b is ultimately converted to bool and printed. That is, the overload std::ostream::operator<<(std::ostream&, void*) isn't applicable and std::ostream::operator<<(std::ostream&, bool) is called instead.
This is because function pointers aren't convertible to void*.
Since the address of a function will never be NULL (i.e., 0), it is treated as true and printed as 1.
phongvants123, I'm not sure what your last post meant, but your b() function is supposed to return an int. Right now you don't have it returning anything.
If you have no use for an int, maybe return a string?
You could change your function to something like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <string>
#include<iostream>
usingnamespace std;
string b();
int main()
{
cout << b() << endl;
return 0;
}
string b()
{
return"need to show on console";
}
b is a variable. (in this case the it is the name of a function, which is 'sort of' a variable in some sense of the word, at the compiler/ assembler level).
b() is a function call result.
printing b (which is a function) here is going to give the address of the function, I think.
b() is correct.