int main()
{
int a=0;
int b=0;
std::cout<<"Please enter an int: "<<std::endl;
std::cin>>a;
auto old_state =std::cin.rdstate(); // remember the current state of cin
std::cin.clear(); // make cin valid
std::cout<<"Please enter another int: "<<std::endl;
std::cin>>b;// use cin
auto new_state = std::cin.rdstate();
if(old_state==std::istream::failbit&&new_state==std::istream::failbit)
//I tried std::ostream,std::iostream,ios_base they all work.
std::cout<<"Both are failbit!"<<std::endl;
}
我刚刚遇到了关于C ++ Primer的IO章节。我自己尝试了这段代码,并且惊讶地发现,在最后两行中,我将std::istream
更改为std::ostream
,std::iostream
,std::ios_base
,当我输入时它们似乎都有效两个char
。有人请帮助用简单的话来解释这为什么会发生这种情况。非常感谢提前!
答案 0 :(得分:3)
因为failbit
是public
的{{1}}成员,所有其他类ios_base
都来自public
。