简体   繁体   English

&在类型后的函数参数中

[英]& in function parameter after type

In a class, we have this: 在课堂上,我们有:

friend Circle copy(const Circle &);

I know that usually to pass something by reference you use & before the name of the variable, but in this case there is no name of variable.....what is this exactly? 我知道通常通过引用传递一些你在变量名称之前使用&,但在这种情况下没有变量的名称.....这究竟是什么?

That is the declaration of a friend function. 这是朋友功能的声明。 In general, when you declare a function, you do not need to name the variables in its argument list. 通常,在声明函数时,不需要在其参数列表中命名变量。 Most people usually do name them, but sometimes it's obvious and they're omitted. 大多数人通常都会给它们命名,但有时它很明显而且它们被省略了。 It doesn't change anything--the name of a variable in a declaration is mere documentation--it's the name in the definition that counts (and can legally differ from the name in the declaration). 它不会改变任何东西 - 声明中变量的名称仅仅是文档 - 它是定义中的名称(并且可以合法地与声明中的名称不同)。

As for what "friend" means in C++, I'll leave you to look that up online. 至于“朋友”在C ++中的含义,我会让你在网上看一下。

It's a function declaration; 这是一个功能声明; you don't need parameter names in those, only in the definition of it. 你不需要参数名称,只在它的定义中。 Somewhere else, there will be something like 在其他地方,会有类似的东西

friend copy(const Circle& other) {
  // method implementation
}

In function declaration mentioning parameters name is not necessary. 在函数声明中,不需要提及参数名称。

friend Circle copy(const Circle &);

Where as in function definition paramerter(s) name is required 在函数定义中,参数名称是必需的

friend Circle copy(const Circle &rhs)
{
.....

}

“功能声明”中不需要参数名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM