什么是朋友ostream [英] What is friend ostream

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

问题描述

对于这一行代码在我的头文件中到底意味着什么,我有些困惑.

I'm a bit confused about what exactly this line of code means in my header file.

friend ostream & operator << (ostream &, const something &);

有人可以为我澄清吗?

推荐答案

该行代码表明运算符<<是某物的朋友(因为它已在某物类定义中列出).这意味着该运算符<<函数可以访问其中的变量.

That line of code says that the operator << is a friend of something (since it's listed in the something class definition). This means that that operator << function can access the variables inside there.

&这里的参数表示您在调用方法时传递对象,而这些参数将只是这些参数对象的另一个名称.返老还童表示您将返回ostream参数,以便可以连接<<表达式一起使用时,避免使用一个全局cout时避免创建新的"cout".

The & here as the parameters mean that you pass in objects when calling the method and those parameters will just be another name for those parameter objects. Returning ostream & means that you're going to return the ostream parameter so that you can connect << expressions together avoiding creating a new "cout" when using the one global cout is what is needed.

这篇关于什么是朋友ostream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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