为什么istream,ostream,iostream,ios_base都在这里工作

时间:2016-01-04 03:56:00

标签: c++ state bit

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::ostreamstd::iostreamstd::ios_base,当我输入时它们似乎都有效两个char。有人请帮助用简单的话来解释这为什么会发生这种情况。非常感谢提前!

1 个答案:

答案 0 :(得分:3)

因为failbitpublic的{​​{1}}成员,所有其他类ios_base都来自public