What are and ?

Started by
3 comments, last by Grantyt3 17 years, 10 months ago
In functions like cout, cin, and the stringstream functions, they aren't just your average function() functions, but they use << and >> instead of parenthesis. Could someone explain what that's all about?
Advertisement
Your question belongs in For Beginners.

They are operators; in the same class of "thing" as +, / etc. They take a stream on the left hand side and some other type on the right hand side. The evaluation of the operator, generally, inputs or outputs the RHS from/to the stream *as a side effect*, and evaluates to a reference to the left hand side (the object may have changed its state as a result of the process, but the "same object" is returned).
They aren't functions at all, they are objects. Specifically stream objects in the STL library. cin and cout are default defined to system input and output. The objects have overloaded the '<<' and '>>' operators to do the same as 'printf()' and 'scanf()', just in a safer friendlier way.
-----------------------------------Indium Studios, Inc.
std::cin is a global variable of type std::istream
std::cout is a global variable of type std::ostream

They aren't functions, they are objects, and thus support operator overloading: << and >> have been overloaded to work with them.

When you write std::cout << 42;, you are really doing operator<<(std::cout, 42);, which ends up calling std::ostream& std::operator<<(std::ostream&, const long& );
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Oh, I see, thanks!

Sorry about putting it in the wrong forum.

This topic is closed to new replies.

Advertisement