什么是和符号,当使用类名后,如ostream& operator ...<<(...)? [英] What's the ampersand for when used after class name like ostream& operator <<(...)?

查看:367
本文介绍了什么是和符号,当使用类名后,如ostream& operator ...<<(...)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道所有的指针和&符号意味着地址,但在这种情况下是什么意思?

I know about all about pointers and the ampersand means "address of" but what's it mean in this situation?

此外,当重载运算符时,为什么通常使用const声明参数

Also, when overloading operators, why is it common declare the parameters with const?

推荐答案

在这种情况下,您将返回对ostream对象的引用。严格地将和号视为地址并不总是为你工作。 这里的参考资料来自C ++常见问题解答。

In that case you are returning a reference to an ostream object. Strictly thinking of ampersand as "address of" will not always work for you. Here's some info from C++ FAQ Lite on references.

对于const,const正确性在C ++类型安全中非常重要,你会尽可能多地做。 FAQ中的另一个页面有助于这方面。 const可帮助您在不需要的情况下从副作用相关的更改中剔除您的数据。

As far as const goes, const correctness is very important in C++ type safety and something you'll want to do as much as you can. Another page from the FAQ helps in that regard. const helps you from side effect-related changes mucking up your data in situations where you might not expect it.

这篇关于什么是和符号,当使用类名后,如ostream& operator ...<<(...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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