为什么流运算符在C ++中返回引用? [英] Why do stream operators return references in C++?

查看:231
本文介绍了为什么流运算符在C ++中返回引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道索引操作符实现通常返回引用,以便可以设置和检索值,但为什么是流?

I know that indexing operator implementations usually return references so that the values can be set as well as retrieved, but why do streams?

推荐答案

因此,您可以将它们链接在一起。

So you can chain them together.

cout << "hello" << "how are you";

工作原因是 cout< hello返回对cout的引用,以便<

Works because cout << "hello" returns a reference to the cout so that << "how are you" knows there to put itself.

大多数操作符,例如 + = ,也可以这样做。

Most operators, such as +=, also do this.

这篇关于为什么流运算符在C ++中返回引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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