小编hae*_*mic的帖子

是否可以在C++ 11中手动设置istream失败位

我做了这堂课:

class object {
  // data...
 public:
  friend std::istream& operator>>(std::istream& in, object& o) {
    char c, d;
    in >> c >> d;
    if (c == d) {
      /*set the fail bit some how*/
      in.putback(d);
      in.putback(c);
    } else
      o.set_data(c, d);
    return in;
  }
};
Run Code Online (Sandbox Code Playgroud)

我正在查看文档(不太好),但找不到设置失败位的正确方法.我关心的原因是我希望能够while(std::cin>>obj)/*do stuff*/;喜欢用int来做.但是,如果我目前这样做,那么每当出现错误时都会有无限循环.-_-设置失败位是否可行,或者我将不得不以不同的方式处理此问题?

c++ iostream friend c++11

8
推荐指数
2
解决办法
1145
查看次数

标签 统计

c++ ×1

c++11 ×1

friend ×1

iostream ×1