The invocation mechanism of while (Cin>>val)

Source: Internet
Author: User
Tags definition bool

Reading today when you see the code while (Cin>>val), suddenly thinking about how to write the legality of how to determine. We all know that CIN is a stream object, and the >> operator returns the stream object on the left, that is, Cin>>val returns CIN, so while (Cin>>val) becomes a while (CIN), The problem becomes the legitimacy of a stream object in the judgment statement.

Whether while (CIN) or if (CIN) is legal, why? We define a class ourselves, then define the object of the class, and then use the IF statement to judge that it is illegal. This shows that the stream object has some kind of transformation function that converts a stream object to a type that the judgment statement can recognize.

The search query on the web found that the stream object does exist in such a transformation.

Open the Iostream.h file and find the definition of CIN, found to be from istream.h, where the template class Basic_istream inherits from Basic_ios, opens the definition of Basic_ios, finds it inherits from Ios_base, and navigates to iOS again _base class and finds that it has two overloaded functions. operator void * () const and BOOL operator! () Const. These two functions make the stream object available as the content of the judgment statement. (Reference page)

operator void * () const, a function that is called while (CIN) or if (CIN), converts a stream object to a void* type.

BOOL operator! () A const function that is invoked when a while (!cin) or if (!cin) converts a stream object to a bool type.

When the stream is in the Goodbit state, read the success code, return the CIN address, and convert to the void* type, continuing the loop  
operator void* () const
{  
    The state of the///stream is changed by the >> operator calling iOS. Set Badbit and Failbit  
    if (state & (Badbit | failbit)///When you read EOF or enter a type that does not conform to the types of writing you return  
        0;/// Returns 0 when the stream is in the Badbit or Failbit state, loop stop  
    return (void*) this;///returns a CIN, equivalent to a while (CIN)  
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

It should be noted that both types of conversions are implicit.

Now that we have found the legal reason for while (CIN), nature needs to be tested.

We define a Class A, define the two functions in a, and then define an object A of a, using if (a) and if (!a) to verify. The code is as follows:

#include <iostream>  
using namespace std;  
    
Class A  
{public  
    :  
        A () {}  
        ~a () {}  
        operator void* () const
        {  
            cout << ' cast to void*; '; return  
            (void *) this;  
        }  
        BOOL operator! () const
        {  
            cout << "cast to bool;";  
            return true;  
        }  
;  
    
int main ()  
{  
    a A;  
    if (a) cout << "a" << Endl;  
    if (!a) cout << "second" << Endl;  
    return 0;  
}

Run the above program, the result is cast to void*; I and cast to bool; Second

The results show that if (a) implicitly invokes the operator void* () function, if (!A) implicitly invokes the BOOL operator! () function.

These two functions are actually overloaded procedures for operators. With this overloaded function, we can use the IF statement to judge our objects, just as with CIN.

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.