简体   繁体   中英

& 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.

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)
{
.....

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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