w*****j 发帖数: 49 | | X****r 发帖数: 3557 | 2 An istream (more precisely, an ios) can be automatic converted
to a void *. The result is a non-null pointer of the state of the stream
is good, or null otherwise. It is then implicitly converted to a bool as
the condition of the while statement, i.e. true if stream good.
【在 w*****j 的大作中提到】 : RT
| h*****0 发帖数: 4889 | 3 我觉得这种写法不好。应该加个函数isGood()之类的(自动转换也是调用函数了的,效
率一样),好读多了。
【在 X****r 的大作中提到】 : An istream (more precisely, an ios) can be automatic converted : to a void *. The result is a non-null pointer of the state of the stream : is good, or null otherwise. It is then implicitly converted to a bool as : the condition of the while statement, i.e. true if stream good.
| t****t 发帖数: 6806 | 4 Usually it is used in context such as
while (cin>>something) {
...
};
as opposed to
while (cin) { // while (cin.isgood())
...
}
In fact, std::basic_ios has a lot of members to check state, such as
good(), eof(), fail(), bad(). You can call them explicitly if you want
to do that (actually bool(cin) == !cin.fail()). But usually this is not
correct:
while (cin) { // or while (!cin.fail())
cin>> something;
...
}
check C++ FAQ lite.
【在 h*****0 的大作中提到】 : 我觉得这种写法不好。应该加个函数isGood()之类的(自动转换也是调用函数了的,效 : 率一样),好读多了。
|
|