Finally understand why C + + istream, such as the return of objects can directly carry out bool judgment

Source: Internet
Author: User

It's been confusing before. C + + with >>, getline and other functions to read data, such as:

        while (CIN>>C) or while (Getline (CIN, str))
Why is it possible to read directly the end of the data or read the data error, which is equivalent to returning a bool type

Because I see the function its prototype is like this, return is a istream&

    istream& operator>> (bool& val);
    istream& operator>> (short& val);
    istream& operator>> (unsigned short& val);
    istream& operator>> (int& val);
    istream& operator>> (unsigned int& val);
    istream& operator>> (long& val);

    istream& Getline (istream& is, string& str, char delim);
    istream& Getline (istream& is, string& str);


The original is its parent class Basic_ios implemented the following two functions, haha, finally understand:

@{
      /**
       *  @brief the  quick-and-easy status check.
       *
       * This allows your to  write constructs such as
       *  "if (!a_stream) ..." and "while (A_stream) ..."
      */
  operator void* () const
      {return This->fail (): 0:const_cast<basic_ios*> (this);}

      BOOL
      operator! () const
      {return this->fail ();}
      //@}



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.