之前一直很困惑c++中用>>, getline等函数读进数据,如:
while(cin>>c) 或者 while(getline(cin, str))
为什么能直接判读是否读到数据尾或者读取数据出错,相当于返回的是个bool类型 因为我看函数其原型是这样的,返回的是个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 );
原来是其父类basic_ios 中实现了以下两个函数,哈哈,终于弄明白了:
//@{ /** * @brief The quick-and-easy status check. * * This allows you 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(); } //@}