1. cout << "hello, " << "world" << endl;
2. "hello, " << "world" << endl;
3. printf("hello, " << "world" << endl);
Why is it that statement 1 is valid but statements 2 and 3 are not?
Hi
Because the standard say so:
ostream& operator<<(ostream&, const char*);
As you know, calling overloaded operator is exactly like calling non-
local or
member function. The above declaration means: get an ostream object
and a char string,
put the char string to ostream object and return it. So when we write:
cout << "Hello, "
the following statement is executed:
operator<<(cout , "Hello, ");
and of course it return the stream object so we can put another string
(in this case "world") to output.
In the following statement:
"hello, " << "world" << endl;
how to say my intend is output operation? We can reason about
statement 3. in the same way.
I hope it helps.
-- Saeed Amrollahi