&符号(&)符号如何在 C++ 中工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8857857/
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-28 19:14:09  来源:igfitidea点击:

how does the ampersand(&) sign work in c++?

c++pointersreferenceampersand

提问by infinitloop

Possible Duplicate:
What are the differences between pointer variable and reference variable in C++?

可能的重复:
C++ 中的指针变量和引用变量有什么区别?

This is confusing me:

这让我很困惑:

class CDummy 
{
public:
   int isitme (CDummy& param);
};

int CDummy::isitme (CDummy& param)
{
  if (&param == this)
  { 
       return true; //ampersand sign on left side??
  }
  else 
  {    
       return false;
  }
}

int main () 
{
  CDummy a;
  CDummy* b = &a;

  if ( b->isitme(a) )
  {
    cout << "yes, &a is b";
  }

  return 0;
}

In C & usually means the address of a var. What does it mean here? Is this a fancy way of pointer notation?

在 C 中 & 通常表示 var 的地址。这里是什么意思?这是一种奇特的指针符号方式吗?

The reason I am assuming it is a pointer notation because this is a pointer after all and we are checking for equality of two pointers.

我假设它是一个指针符号的原因是因为这毕竟是一个指针,我们正在检查两个指针​​是否相等。

I am studying from cplusplus.com and they have this example.

我正在从 cplusplus.com 学习,他们有这个例子。

回答by Luchian Grigore

The &has more the one meanings:

&有更多的一个含义:

1) take the address of a variable

1) 取一个变量的地址

int x;
void* p = &x;
//p will now point to x, as &x is the address of x

2) pass an argument by reference to a function

2) 通过引用函数传递参数

void foo(CDummy& x);
//you pass x by reference
//if you modify x inside the function, the change will be applied to the original variable
//a copy is not created for x, the original one is used
//this is preffered for passing large objects
//to prevent changes, pass by const reference:
void fooconst(const CDummy& x);

3) declare a reference variable

3) 声明一个引用变量

int k = 0;
int& r = k;
//r is a reference to k
r = 3;
assert( k == 3 );

4) bitwise and operator

4) 按位和运算符

int a = 3 & 1; // a = 1

n) others???

n) 其他人???

回答by rwols

To start, note that

首先,请注意

this

is a special pointer ( == memory address) to the class its in. First, an object is instantiated:

是一个指向它所在类的特殊指针(==内存地址)。首先,一个对象被实例化:

CDummy a;

Next, a pointer is instantiated:

接下来,实例化一个指针:

CDummy *b;

Next, the memory address of ais assigned to the pointer b:

接下来,将 的内存地址a分配给指针b

b = &a;

Next, the method CDummy::isitme(CDummy &param)is called:

接下来,该方法CDummy::isitme(CDummy &param)被调用:

b->isitme(a);

A test is evaluated inside this method:

在此方法中评估测试:

if (&param == this) // do something

Here's the tricky part. param is an object of type CDummy, but &paramis the memory address of param. So the memory address of param is tested against another memory address called "this". If you copy the memory address of the object this method is called from into the argument of this method, this will result in true.

这是棘手的部分。param 是 CDummy 类型的对象,但它&param是 param 的内存地址。因此,param 的内存地址是针对另一个名为“ this”的内存地址进行测试的。如果将调用此方法的对象的内存地址复制到此方法的参数中,这将导致true.

This kind of evaluation is usually done when overloading the copy constructor

这种评估通常在重载复制构造函数时完成

MyClass& MyClass::operator=(const MyClass &other) {
    // if a programmer tries to copy the same object into itself, protect
    // from this behavior via this route
    if (&other == this) return *this;
    else {
        // otherwise truly copy other into this
    }
}

Also note the usage of *this, where thisis being dereferenced. That is, instead of returning the memory address, return the object located at that memory address.

还要注意*this, wherethis取消引用的用法。也就是说,不是返回内存地址,而是返回位于该内存地址的对象。

回答by Hoons

Well the CDummy& paramthat is declared as a parameter of the function CDummy::isitmeis actually a referencewhich is "like" a pointer, but different. The important thing to note about references is that inside functions where they are passed as parameters, you really have a reference to the instance of the type, not "just" a pointer to it. So, on the line with the comment, the '&' is functioning just like in C, it is getting the address of the argument passed in, and comparing it to thiswhich is, of course, a pointer to the instance of the class the method is being called on.

那么CDummy& param声明为函数参数的CDummy::isitme实际上是一个引用,它“像”一个指针,但不同。关于引用需要注意的重要一点是,在将它们作为参数传递的函数内部,您确实拥有对类型实例的引用,而不仅仅是“只是”指向它的指针。因此,在注释行中,'&' 的功能就像在 C 中一样,它正在获取传入参数的地址,并将其与this当然是指向类实例的指针进行比较方法正在被调用。