Sign in to follow this  
Grantyt3

What are and ?

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this