I'm trying to understand what's really happening when I compile and execute C++ code, but the line cout << "output";
has me a bit confused.
I know that the <<
, operator is the bitwise leftshift operator, and that executing y = x << 6
will assign the value to y
that resulted from shifting x
开发者_高级运维to the left by six bits.
I also know that '<<', with respect to streams, is the insertion operator, and that executing cout << "output";
inserts the string output
into the object cout
.
What I want to know is whether this is an example of the overloading of <<
, or if cout
really is being shifted to the left by a value that corresponds to the number of bits occupied by the string output
. If the output
really is just being inserted into cout
via the overloading of <<
, then why has the bitwise operator been used rather than the assignment operator =
, which would be rather more intuitive?
Question: How does cout << "output"
place the word "output" on my terminal screen?
In a word, the << operator is overloaded (a very common c++ feature).
Just like other methods, operators may be overloaded, and as hinted by Martin York in his response, operators are little more than methods which the compiler invokes when it parses an operand and operator expression.
What happens is that when applied to operands of type integer and such << has the "typical", bitwise operator semantics, when applied to a stream it has "printf-like" semantics.
cout is a object of type ostream. See details for its ostream::operator<<
Effectively the ostream::operator<< is overaloaded multiple times, for each of the possible types of the its second argument (on its right side). This allows feeding a stream with various types and not having to specify a format. This operator returns an ostream which allows chaining several << together.
The reason why << was chosen for the ostream operator is that is allows showing in the line code things in the same order as they will appear in the output:
cout << "Found " << nbOfCats << " cats in the " << barnName << "barn.";
this is thought to be easier to read, and is also less error prone than say
printf("Found %ld cats in the %s barn.", nbOfCats, barnName);
Yes, it's overloading.
The <<
operator was used for output because it, together with >>
for input, has mnemonic value (easy to remember) and low precedence (thus allowing most kinds of expressions as arguments without added parentheses).
What goes on in detail is actually pretty complicated. The one thing that sometimes baffles newcomers to C++ is that some of the overloads of <<
are freestanding functions, and some non-static member functions. E.g. when you construct a temporary ostringstream
object then using <<
, this call can only be resolved to member function overload because a temporary object can't be bound to the formal reference-to-non-const
argument of the freestanding function overloads. Why it is that way, I think nobody knows, possibly just bad history. I seem to recall that some of it was fixed in C++0x, but I'm not sure (check it if important).
Cheers & hth.,
This is confusing because the bitwise operator is not actually being used in this case. They used operator overloading simply because the << looks good and readable in code.
It does not place the word "output" on the terminal screen, it places it in the buffer first. When the buffer flushes, only than appears the word on the terminal screen.
Streams:
std::cout << std::string("Plop");
This is syntactic sugar for calling a method:
std::ostream& operator<<(std::ostream& str, std::string const& data)
{
// Do Stuff
return str;
}
How the Do stuff does its thing is not defined by the standard it just works.
But probably it just does:
write(2, data.c_str(), data.length());
Where 2 is the file descriptor for the output stream. The output stream is connected to the application by the OS so when an application writes to this stream the OS can read the data. What the OS does with the data is completely dependent on the situation.
But if you started the application from the command line it probably connected the out stream from the application to the terminal, it is then the responsibility of the terminal on what to do with the data (ignore it, print it etc) and depends completely on the terminal application.
精彩评论