what is the difference between cout.operator<<("Hellow World");
and
operator<<(cout, "Hellow World")
;
When you write cout.operator<<("Hellow World");
you are calling the operator<<
method and passing a const char *
. Because this method is not overloaded to take a const char *
, but is overloaded to take a const void *
, that's the one you call and get 0x8048830
(which is the address of the 'H'
character in your string).
When you write operator<<(cout, "Hellow World")
you are calling a free function operator<<
that takes an output stream on the left-hand side and a const char *
on the right-hand side. There happens to exist such a function, and it has the following declaration (simplified):
std::ostream& operator<<(std::ostream& os, const char *s);
And so you get printed the sequence of characters that begin at s
, and end at the first '\0'
that is encountered (so you get Hellow, world
).
Finally, when you write std::cout << "Hello, world" << std::endl;
, since you are passing a const char *
to operator<<
, the free function, being a perfect match, is chosen over the method operator<<
.