English 中文(简体)
浏览器(&)如何在C++上签字? [复制]
原标题:how does the ampersand(&) sign work in c++? [duplicate]
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
What are the differences between pointer variable and reference variable in 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?

我之所以假设这是个点,是因为这是毕竟的一点点,我们正在检查两个点人的平等。

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

最佳回答

To start, note that

this

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

CDummy a;

接下来,一个要点是:

CDummy *b;

其次,<代码>a的记忆地址分配给点人<代码>b:

b = &a;

其次,以下方法称为:

b->isitme(a);

这种方法对测试进行了评估:

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

Here s the tricky part. param is an object of type CDummy, but &param is 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.

这种评价通常是在超载影印构件时进行的。

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
    }
}

还注意到<条码>* 原文 ,其中<条码> >> 原文 正在上注明>。 这不是归还记忆地址,而是归还位于该记忆地址的物体。

问题回答

<代码>& 更具有以下含义:

1) take the address of a variable

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

(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) 申报参考变量

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

4)双向和操作者

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

无?

Well the CDummy& param that is declared as a parameter of the function CDummy::isitme is actually a reference which 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 this which is, of course, a pointer to the instance of the class the method is being called on.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签