我正在尝试学习 C++,并在我正在使用的教程中遇到了这个令人困惑的示例,也许有人可以为我澄清这一点。
以下代码来自教程。我已经放了一个“标签”(如果你愿意的话// <---),指出让我感到困惑的部分。
#include <iostream>
using namespace std;
class CDummy {
  public:
    // signature: accepts an address 
    int isitme (CDummy& param);
};
// actual function that accepts an address passed in from main set to
// the param variable
int CDummy::isitme (CDummy& param) // <--- 1
{
现在这是令人困惑的部分。我正在获取地址变量的地址???地址不是已经传进去了吗?
  if (¶m == this) return true; // <--- 2
  else return false;
}
int main () {
  CDummy a;
  CDummy* b = &a;
  // passes in the class 
  if ( b->isitme(a) )
    cout << "yes, &a is b";
  return 0;
}
现在在下面对我有意义的代码中
#include <iostream>
using namespace std;
class CDummy {
  public:
    int isitme (CDummy *param);
};
int CDummy::isitme (CDummy *param) // <--- 1
{
这部分非常有意义。param 是一个指针,我将 a 类的指针与 b 类的指针进行比较。
  if (param == this) return true; // <--- 2
  else return false;
}
int main () {
  CDummy a;
  CDummy *b = &a;
  // pass in the address.
  if ( b->isitme(&a) )
    cout << "yes, &a is b";
  return 0;
}
哪一个代码示例是正确的?从某种意义上说是正确的,这是首选的方法,因为它们都有效。另外,为什么我在第一个示例中使用地址的地址?
抱歉,如果以前已经回答过这个问题,但我找不到。谢谢