Jun 17, 2012 at 6:17pm UTC
Hi,
I am pretty curious how the if statement find out true or false from a iostream object as the following:
if (cin)
{
...
}
How could C++ achieve this?
Thanks.
Jun 17, 2012 at 8:30pm UTC
In what condition would you expect it to return true or false?
For the get function:
http://cplusplus.com/reference/iostream/istream/get/
This use of the function which you have shown will return the character which was read. This means that if you type ANYTHING, it will return non-zero which means it will always return
true
.
Last edited on Jun 17, 2012 at 8:31pm UTC
Jun 17, 2012 at 9:57pm UTC
In c++, any non-zero value is interpreted as true.
Jun 17, 2012 at 10:02pm UTC
The class istream has a conversion member function that converts an istream object to const void *
So then you use cin such a way as
if ( cin )
cin is converted to const void * and then it is compared with zero.
Last edited on Jun 17, 2012 at 10:02pm UTC