Consider the following code:
Code:
#include <iostream>
int main()
{
using namespace std;
cout << 'a' << endl << 'b' << endl;
cout.operator <<('a');
cout << endl;
cout.operator <<('b');
cout << endl;
cout.operator <<(*"ab");
cout << endl;
cout.operator <<("ac");
cout << endl;
operator <<(cout, "ac");
cout << endl;
return 0;
}
The output looks like this:
a
b
97
98
97
00B57800
ac
where the '00B57800' is just some memory location. That's also the line that surprized me. I was expecting to see 'ac' there because I had thought that 'cout << "ac";' was just shorthand for 'cout.operator <<("ac");'
Since that's obviously not the case, I looked around in Stroustrup 2009 and found on p. 610:
basic_ostream& operator<<(const void* p)
So, that explains the output of a memory location, and one of the template classes further down on the same page explains why operator <<(cout, "ac"); does give the desired 'ac' output.
What I'm wondering, though, is how the compiler knows to interpret 'cout << "ac";' as 'operator <<(cout, "ac");' rather than as 'cout.operator <<("ac");'