While (CIN)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.