return value of cout and cin

Started by
4 comments, last by TEUTON 17 years, 12 months ago
Does cout return 0 and 1 on failure and success or something else.???
Advertisement
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).
So, basically refrences to themselves.
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
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.
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.

This topic is closed to new replies.

Advertisement