1

I'm trying to learn C++ and ran into this confusing example in the tutorial I'm using perhaps someone can clarify this for me.

The following code is from the tutorial. I've put a "tag" (if you will // <--- )indicating the parts that are confusing to me.

#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
{

Now here is where the confusing part comes. I'm taking the address of an address variable??? Wasn't the address already passed in?

  if (&param == 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;
}

Now below in the code that makes sense to me

#include <iostream>
using namespace std;

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

int CDummy::isitme (CDummy *param) // <--- 1
{

this part makes perfect sense. param is a pointer and I'm comparing the pointer of class a with the pointer of class 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;
}

Which one of the code samples is correct? Correct in the sense that this is the preferred way to do it, because they both work. Also, why am I taking an address of an address in the first example?

Apologies if this has been answered before but I couldn't find it. Thanks

lorddarkangel
  • 212
  • 3
  • 21
Jeff
  • 6,932
  • 7
  • 42
  • 72
  • Two different meanings for the symbol `&`: one as an operator and one as part of a type declarator. `&i` is the address of `i`, but `int& i` is declaring `i` to be a reference to int. – Joseph Mansfield Dec 25 '12 at 19:59

3 Answers3

4
int CDummy::isitme (CDummy& param) // <--- 1

In C++, the & means to pass the parameter by reference rather than by value. It doesn't indicate an address.

Note that you might see CDummy & param or CDummy &param. It's all the same.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
3

You need to take you favorite C++ book and read about references.

In this declaration

int CDummy::isitme (CDummy& param)

param is not an address, as you seem to believe. It is a reference.

You are not the first one to be misled by the & character used in reference declarations. This question has been asked many times before on StackOverflow

What is a reference variable in C++?

What are the differences between a pointer variable and a reference variable in C++?

but a book/guide/tutorial will make a better starting point, in my opinion.

Community
  • 1
  • 1
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
1

The unary & returns the address of its operand. And when used in the parameters, it indicates a pass by reference.

David G
  • 94,763
  • 41
  • 167
  • 253