C++ 强制类型转换运算符的重载 (例题while(m >> n1 >> n2))

样例输入
12 44
344 555
-1
2 3
样例输出
12 44
344 555

#include 
using namespace std;
class MyCin
{
    public:
        bool n;
        MyCin() { n = 1; }
        MyCin&operator>>(int &a) {
            cin >> a;
            if(a==-1)n=0;
            return *this;
        }
        operator bool() {
             return n;
        }
};



int main()
{
    MyCin m;
    int n1,n2;
    while( m >> n1 >> n2) 
        cout  << n1 << " " << n2 << endl;
    return 0;
}

你可能感兴趣的:(C++,运算符重载,强制类型转换运算符)