C++ 变量等末尾的与号 (&)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20864574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:21:59  来源:igfitidea点击:

ampersand (&) at the end of variable etc

c++referencefriend

提问by snipor

I am a C++ noob and i've a problem of understanding c++ syntax in a code. Now I am quite confused.

我是一个 C++ 菜鸟,我在理解代码中的 C++ 语法时遇到了问题。现在我很困惑。

class date
{
private:
int day, month, year;
int correct_date( void );
public:
void set_date( int d, int m, int y );
void actual( void );
void print( void );
void inc( void );
friend int date_ok( const date& );
};

Regarding to the '&' character, I understand its general usage as a reference, address and logical operator...

关于“&”字符,我理解它作为引用、地址和逻辑运算符的一般用法......

for example int *Y = &X

例如 int *Y = &X

What is the meaning of an & operator at end of parameter?

参数末尾的 & 运算符是什么意思?

friend int date_ok( const date& );

Thanks

谢谢

edit:

编辑:

Thanks for the answers. If I have understood this correctly, the variable name was simply omitted because it is just a prototype. For the prototype I don't need the variable name, it's optional. Is that correct?

感谢您的回答。如果我理解正确的话,变量名被简单地省略了,因为它只是一个原型。对于原型我不需要变量名,它是可选的。那是对的吗?

However, for the definition of the function I definitely need the variable name, right?

但是,对于函数的定义,我肯定需要变量名,对吗?

回答by Aniket Inge

const date&being accepted by the method date_okmeans that date_oktakes a reference of type const date. It works similar to pointers, except that the syntax is slightly more .. sugary

const date&被方法接受date_ok意味着date_ok需要一个类型的引用const date。它的工作原理类似于指针,除了语法稍微更......

in your example, int* Y = &xmakes Ya pointer of type int *and then assigns it the address of x. And when I would like to change the value of "whatever it is at the address pointed by Y" I say *Y = 200;

在您的示例中,int* Y = &x创建Y一个 type 指针,int *然后为其分配x. 当我想更改“指向的地址中的任何内容”的值时,Y我说*Y = 200;

so,

所以,

int x = 300;
int *Y = &x;
*Y = 200; // now x = 200
cout << x; // prints 200

Instead now I use a reference

相反,现在我使用参考

int x = 300;
int& Y = x;
Y = 200; // now x = 200
cout << x; // prints 200

回答by Lightness Races in Orbit

In this context, &is not an operator. It is part of the type.

在这种情况下,&不是运算符。它是类型的一部分。

For any given type T, the type T&is a "reference to T".

对于任何给定的类型T,该类型T&是“对”的引用T

The symbol &in fact has three meanings in C++, and it's important to recognise those different meanings.

符号&实际上在 C++ 中具有三种含义,识别这些不同的含义很重要。

  • "address of" when applied to an expression
  • "reference" when part of a type
  • "bitwise AND" when applied to two numbers
  • 应用于表达式时的“地址”
  • 类型的一部分时的“引用”
  • 应用于两个数字时的“按位与”

Similarly, *has at least three meanings, and once you've grasped those, you'll have pointers and references down. :-)

同样,*至少有三个含义,一旦你掌握了它们,你就会有指针和引用。:-)

If I have understood this correctly, the variable name simply was omitted there because it is just the prototype. And for the prototype i don't need the variable name, it's optional. Is that correct?

如果我理解正确的话,变量名在那里被简单地省略了,因为它只是原型。对于原型,我不需要变量名,它是可选的。那是对的吗?

Yes.

是的。

However, for the definition of the function I need definitely the variable name, right?

但是,对于函数的定义,我肯定需要变量名,对吗?

No. Although you'll usually want it (otherwise what's the point?!) there are some circumstances in which you don't, usually when you've only introduced the parameter to engage in overload-related trickery.

不。虽然您通常会想要它(否则有什么意义?!)但在某些情况下您不想要它,通常是当您只引入参数来从事与重载相关的技巧时。

But speaking purely technically you can omit the argument name from the declaration and/or the definition as you wish.

但纯粹从技术上讲,您可以根据需要从声明和/或定义中省略参数名称。

回答by Mark

So, for starters, I think you might be more confused in that the author of that code omitted something quite important (although optional): the variable name.

因此,对于初学者,我认为您可能会更困惑,因为该代码的作者省略了一些非常重要的(尽管可选):变量名称。

Let's rewrite that:

让我们重写一下:

friend int date_ok( const date& check);

The type of the variable 'check' is const date&. We are passing it to the function as a 'constant reference'. In other words, it's an alias to whatever we passed in (via pointer magic), but we cannot modify it.

变量 'check' 的类型是const date&。我们将它作为“常量引用”传递给函数。换句话说,它是我们传入的任何东西的别名(通过指针魔法),但我们不能修改它。

The reason that we do this is so that we can pass large objects (like a std::vector) into functions without making a copy of them. Pass by value incurs a copy operation. For an int, that doesn't matter (it takes next to no time), for a class, it might be more significant. The rule of thumb is to always pass objects by reference, and to pass them by const reference if you don't intend to modify them. (This rule of thumb ignores move semantics, but I'll assume you don't know about that yet).

我们这样做的原因是我们可以将大对象(如 a std::vector)传递给函数,而无需复制它们。按值传递会导致复制操作。对于 int,这无关紧要(几乎不需要时间),对于一个类,它可能更重要。经验法则是始终通过引用传递对象,如果您不打算修改它们,则通过常量引用传递它们。(这个经验法则忽略了移动语义,但我假设你还不知道)。

回答by Stephane Rolland

using & at the end of a type in a function prototype allows passing-by-reference, instead of passing-by-value (copy). This way you can modify the date object in the friend function.

在函数原型中的类型末尾使用 & 允许按引用传递,而不是按值传递(复制)。这样你就可以修改友元函数中的日期对象。

friend int date_ok( const date& );

Friend: In your class definition this mean that you're telling that a function date_okcan access to all parameters of your class. In deed it means that it is almost a member of your class, so consider adding this friend function rather as a member function. (unless you have other goodreasons, like not polluting your class with foreign definitions)

朋友:在你的类定义中,这意味着你告诉一个函数date_ok可以访问你的类的所有参数。实际上,这意味着它几乎是您的类的成员,因此请考虑将这个友元函数添加为成员函数。(除非你有其他充分的理由,比如不要用外国定义污染你的班级)

If yes, also consider making that a static function, it could have all the same access to the guts of your date object class. But that would be more natural.

如果是,还可以考虑将其设为静态函数,它可以完全相同地访问日期对象类的内容。但这会更自然。

cf. the book "101 C++ coding standard". Prefer defining static member functions, it favours loose coupling.

参见 《101 C++编码标准》一书。更喜欢定义静态成员函数,它有利于松散耦合。

回答by snipor

Thanks for the answesers. If I have understood this correctly, the variable name simply was omitted there because it is just the prototype. And for the prototype i don't need the variable name, it's optional. Is that correct?

感谢您的回答。如果我理解正确的话,变量名在那里被简单地省略了,因为它只是原型。对于原型,我不需要变量名,它是可选的。那是对的吗?

However, for the definition of the function I need definitely the variable name, right?

但是,对于函数的定义,我肯定需要变量名,对吗?