[英]use of ampersand on left hand side of scope resolution operator?
I'm trying to understand a piece of code. 我正在尝试理解一段代码。 I know how to use scope resolution operator.
我知道如何使用范围解析运算符。 The syntax is
语法是
return-type class-name::operator+(argument list)
In my code I have the following form 在我的代码中,我具有以下形式
const Rectangle & Rectangle ::operator =(const Rectangle & rhs)
I want to know that what is the purpose of using & on the left hand side of scope resolution operator. 我想知道在作用域解析运算符左侧使用&的目的是什么。
Lets take that argument , it's declared as 让我们接受该参数 ,它声明为
const Rectangle & rhs
The argument name is rhs
, it's type is const Rectangle &
. 参数名称为
rhs
,类型为const Rectangle &
。
Now lets go back to the return type. 现在让我们回到返回类型。 The return type is
返回类型为
const Rectangle &
Note that the ampersand is part of the return type . 请注意,“&”号是返回类型的一部分 。
It might be easier to see if it's written as 可能更容易看出它是否写为
const Rectangle&
(without space between Rectangle
and the &
). (在
Rectangle
和&
之间没有空格)。
That means the function returns a reference to a constant Rectangle
object. 这意味着该函数返回对常量
Rectangle
对象的引用。
Perhaps it's easier to illustrate using your 也许使用您的插图更容易说明
return-type class-name::operator =(argument list)
Placing the function and the above line next to each other (with some added spacing) we have: 将函数和上面的行彼此相邻放置(具有一些增加的间距),我们得到:
return-type class-name::operator =(argument list)
const Rectangle & Rectangle ::operator =(const Rectangle & rhs)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.