On Sep 19, 2:40 pm, Erik Wikstr??m <Erik-wikst...@telia.com> wrote:
On 2007-09-19 08:37, subramanian10...@yahoo.com, India wrote:
Consider the code:
#include <iostream>
using namespace std;
int main( )
{
cout << "test string ";
cout.operator<<(10).operator<<(endl);
return 0;
}
This prints
test string 10
followed by a newline as expected.
However, in main( ), if I have
cout.operator<<("test string
").operator<<(endl).operator<<(10).operator<<(endl);
it prints
0x804897c
10
followed by a newline. It does not print test string but instead
prints its address.
I do not understand.
Does it mean that operator<<(const char *) is not a member function of
ostream ? If so why ?
You are correct, but as to why it is not a member I do not know. Last
time I saw this discussed none seemed to be able to come up with a good
reason, you could try asking in comp.std.c++ though.
--
Erik Wikstr??m
I just now found a member function
operator<<(const void * val)
Could the reason be the following :
If we wanted to print some address, we have to call
cout.operator<<(ptr) EXPLICITLY for any pointer ptr. So, by calling
cout.operator<<("test string"), the compiler assumes that we want to
print the address and so it prints 0x804897c
When we give cout << "test string", the overloaded non-member function
is called to print the string literal itself.
Is this reasoning correct ? Excuse me if I am wrong.
which would not work otherwise.