Why does << operator work only when there is at least one cout?

  • Thread starter Mc Lauren Series
  • Start date
M

Mc Lauren Series

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?
 
A

amrollahi.saeed

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
 
J

joshuamaurice

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?

You need a little more firm understanding of the language to really
understand why. Basically, you need to pick up a decent book on teach
yourself C++, or an excellent website, etc.

The short answer is because the language rules say so.

A slightly longer answer is that cout is an object which overloads the
operator<< to do formatted output to the standard out of the process,
which most implementations put to your screen.
 
T

Tim Slattery

Mc Lauren Series said:
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?

Because it's a function of the cout object. It causes the operand on
the right side to be output, and it returns the cout object, so that
it can be chained, as in line 1.
 
J

James Kanze

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?

Because there is an operator<< defined with an std::eek:stream (the
type of std::cout) as left argument, but not for char const[]
(the type of a string literal).

What should 2 or 3 mean?
 
J

James Kanze

Because it's a function of the cout object.

Actually, it's not. It's a free function. (At least in
standard C++. In the classical iostream, before the standard,
it was a member.)

The important point is that 1) there is a << operator defined
which takes an std::istream (the type of std::cout) as its left
argument, and 2) as you point out, the result of this operator
is also an std::istream, so you can chain the operations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top