-2
#include <iostream>
using namespace std;

class Dummy {
  public:
    bool isitme (Dummy& param);
};

bool Dummy::isitme (Dummy& param)
{
  if (&param == this) return true;
  else return false;
}

int main () {
  Dummy a;
  Dummy* b = &a;
  if ( b->isitme(a) )
    cout << "yes, &a is b\n";
  return 0;
}

I was looking at this C++ example and I don't understand why bool isitme (Dummy& param); uses the dereferencing sign '&'. The argument is an Dummy object itself right, why it is the object's address?

Jiang Wenbo
  • 469
  • 2
  • 9
  • 20

2 Answers2

3

Ampersand is not the "dereferencing sign". It is used here in two different ways which I will explain below.

The "dereference sign" is asterisk (*).

  1. bool Dummy::isitme (Dummy& param) Here, Dummy& param means that param is a reference to a Dummy object.

  2. if (&param == this) return true; Here, &param denotes address of param.

nakiya
  • 14,063
  • 21
  • 79
  • 118
0

The isitme takes its argument by reference that is it takes the address of the object instead of copying it. The point is not to copy the object and to work on the original one.

navyblue
  • 776
  • 4
  • 8