cin.get(ch)在EOF返回什么 [英] What does cin.get(ch) return at EOF

查看:276
本文介绍了cin.get(ch)在EOF返回什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题有点微不足道,但我想理解为什么这段代码有效

  char  ch; 
if (cin.get(ch)!= 0
{
cout<< CH;
}



我知道cin.get(ch)通过引用返回cin但这意味着它不能返回0或NULL,因为它们是常量所以,为什么这段代码是完全合法的吗?



谢谢你,Sam。

解决方案

istream :: get已记录 [ ^ ],毕竟。

它返回(在你的情况下)对 istream的引用对象本身( * this )。

如文档中所述:

请注意,可以检查此返回值对于流的状态(请参阅将流转换为bool以获取更多信息)。



(链接到'更多信息' [ ^ ])。


Hi, My question is somewhat trivial but I want to understand why this code works

char ch;
if (cin.get(ch) != 0)
{
    cout << ch;
}


I know that cin.get(ch) returns cin by reference but that means it could not return 0 or NULL as they are constants so, Why this code snippet is perfectly legal ?

Thanks, Sam.

解决方案

istream::get is documented[^], after all.
It returns (in your case) a reference to the istream object itself (*this).
As specified in the documentation:

Note that this return value can be checked for the state of the stream (see casting a stream to bool for more info).


(link to 'more info'[^]).


这篇关于cin.get(ch)在EOF返回什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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