为什么"cin>"作为条件 [英] why "cin >>" works as a condition

查看:39
本文介绍了为什么"cin>"作为条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用 cin>> 作为条件.但是我无法理解它的功能,因为它不同于典型的条件表达式,例如 a<b .为什么它可以作为条件?

I know I can use cin >>as a condition. But I can't understand its functioning because it is unlike typical condition expressions like a a < b. Why it can works as a condition ?

推荐答案

cin>>返回 cin .放入 if 时,将调用 istream operator bool ,返回 cin.good().查看 ios :: operator bool的文档.

cin >> a return cin. When you put in in the if, the operator bool of istream is called, which return cin.good(). Look at The documentation of ios::operator bool.

这与 if((bool)(cin>> a)) if((cin>> a).good()).

这篇关于为什么"cin&gt;"作为条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆