In comp.os.linux.advocacy, Suzie
<
[email protected]>
wrote
Hi guys,
I've been coding in C for several years and I'm getting started with
C++. I was given the following exercise to do and I don't quite
understand it. I was wondering if any of you guys could help.
#include <iostream>
using namespace std;
int main() {
cout << "hello world";
return 0;
}
Why is cout being shifted left "hello world" times?
David Lindauer posted an excellent response to this, but
neglected to mention that the function signature you're
confused about can be written in two forms, and some
usage examples (if I'm not too far off).
cout is a std:

stream_withassign (I think); most will see it
as a std:

stream, and it can be passed around as such. The
'_withassign' I'd have to research; presumably it has to do with
overloading of the *assignment* operator.
So the first form is the one above, or, written in "longhand inline"
('using namespace std' allows one to drop the 'std::' prefix):
std::cout << "Hello, world!" << std::endl;
where std::endl is a convenient (and standard) way to end a line,
if one doesn't like to use "\n".
But there's a second, more ungainly (but more in accord with standard
C syntax) form:
operator<<( std::cout, "Hello, world!");
if I'm not mistaken. This corresponds to the function signature
std:

stream & operator << (std:

stream & stream, const char * str_ptr);
There are corresponding operators for int, double, and const void *
(void * simply prints out a hex representation of the pointer, and
is primarily for debugging use), and a fair number of others
as well. Their signatures are as expected:
std:

stream & operator << (std:

stream & stream, int intval);
std:

stream & operator << (std:

stream & stream, double doubleval);
std:

stream & operator << (std:

stream & stream, const void * arb_ptr);
or something like that; I'd have to look.
The metaphor of "shifting" is rather apt, as it turns out; the usual
shift operator
1 << 4
shifts the left value 4 bits, and returns the result (16). The
std:

stream & operator << (std:

stream & stream, const char * str_ptr);
might be said to "shift" the right operand into the stream -- such
usage is occasionally seen in contexts such as parsers, albeit in
the other direction -- and yes, there's a std::cin (or cin) and
one can use std::cin >> inputval, although not nearly as frequently.
And now, of course, one can do things such as:
std::cout << "We now have " << count << " widgets in inventory, "
<< numDamaged << " of which were damaged." << std::endl;
which prints out things like
We now have 10 widgets in inventory, 2 of which were damaged. [endline]
If one has a class, one can declare '<<' and '>>' on it, as in
the following example:
class PrisonerOfWar ...
{
public:
std::string name() const { ... }
std::string rank() const { ... }
long serialNumber() const { ... }
...
};
std:

stream & operator << (std:

stream & out, PrisonerOfWar const & val)
{
out << val.name() << " " << val.rank() << " "
<< val.serialNumber();
return out;
}
Followups reset to the C++ group; this is not a Linux-specific issue
(although there are minor differences in compilers).