Sign in to follow this  
TEUTON

return value of cout and cin

Recommended Posts

the << and >> operators of cout and cin return references to cout and cin, which is why you can chain the << and >> operators.

In other words
cout << 1 << "test";

becomes

cout.operator<<(1).operator<<("test");

And since cout.ooperator<< returns a reference to cout after printing the first value it effectively becomes

cout.operator<<("test");

And after that prints, it returns a reference to cout which isn't used for anything (the most common use of the returned reference, that I'm aware of is for writing overloaded operator<< and >> functions for printing and reading your own classes).

Share this post


Link to post
Share on other sites
cin and cout are objects - they don't have return values. However, they have a lot of methods and overloaded operators that returns something. If you want to know what these methods returns to you - look it up:
http://www.cppreference.com/cppio/index.html
http://msdn2.microsoft.com/en-us/library/zae2h4f4(vs.80).aspx

Share this post


Link to post
Share on other sites
Quote:
Original post by TEUTON
So, basically references to themselves.


When using the << and >> operators on them then yes those functions do.

As Paulius Maruska said cout and cin are objects which don't have return values. I just assumed your were talking about the << and >> operators in particular.

Share this post


Link to post
Share on other sites
Actually I got confused that is why asked this question.

I usually write code like

if(!cin>>x)
{
}
else
{
}

So was thinking about return value of cin.
But now it's clear. Thank You.

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