Jump to content
  • Advertisement
Sign in to follow this  
TEUTON

return value of cout and cin

This topic is 4611 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

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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!