Jery's earliest intention was to support a pure test of an iostream class object, like this:
If (CIN )...
To enable CIN to obtain a real value, Jerry first defines a conversion operator for it, namely, operator int (). In the case of good behaviors such as above, it can indeed run correctly. But in the following error program design, its behavior will be puzzling:
Cin <intval;
Cout rather than CIN is what programmers need. The "type-safe" nature of the class hierarchy should be able to capture the incorrect use of such output operators. However, the compiler may do this:
Int temp = cin. Operator int ();
Temp <intval;
This is not what we want.
How can this behavior be solved? Jerry expected to replace operator int () with operator void ().
Operator void * () indicates whether stream has an error (equivalent! Fail ())
Operator! () Significance: whether stream has an error (equivalent to fail ())
We can use operator void * () to check the current status of stream in the control structure:
While (STD: CIN)
{
...
}
The Boolean condition value in the control structure does not have to be directly converted to bool, as long as it can be converted toInteger typeOrPointer typeThat's enough.
If you use CIN to read an integer but enter a character, CIN sets a failbit to indicate the invalid bit. Operator void * returns 1 and operator when the stream is normal! Returns 0.