• Advertisement
Sign in to follow this  

What are and ?

This topic is 4333 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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& );

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement