在C++中返回对 istream 的引用
Returning a reference to istream in C++
我在加速C++上读到了这一点。这是一个简化版本。
istream& read_hw(istream& in, Student_info& s)
{
in >> s.name >> s.midterm >> s.final;
return in;
}
然后,我们可以调用该函数为:
Student_info s;
read_hw(cin, s);
我的问题是,
- 返回对 istream 的引用有什么意义?由于这两个参数都是通过引用传递的;
- 在调用函数时,我们似乎并不关心返回值
你应该阅读下一段:
返回流允许我们的调用方写入
if (read_hw(cin, homework)){/*...*/}
作为缩写
read_hw(cin, homework); if (cin) {/*...*/}
返回对istream
的引用将启用级联。例如:
int i, j;
std::cin >> i >> j;
// Equivalent to std::cin.operator>>(i).operator>>(j);
istream::operator>>()
返回istream&
,以便级联>>
正常工作。
相关文章:
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- 为什么istream不支持右值提取
- 将无符号char*转换为std::istream*C++
- c++r值引用应用于函数指针
- C++,如何根据运行时条件构造引用不同 istream 对象的对象?
- 引用另一个类时的 iStream 重载
- 基于条件的iStream引用
- 为什么 istream 类的运算符<<在 C++ 中具有引用的返回/参数类型?
- 在C++中返回对 istream 的引用
- 为什么我们需要在重载>>和<<运算符时返回对 istream/ostream 的引用?
- 我使用 istream 获得未初始化的引用成员
- 为什么不能重新分配给对 istream / ostream 的引用
- 如何比较两个 std::istream 引用
- 使用std::istream引用初始化初始化初始化