0

Some of methods that I see in C++ code are of this structure:

void Class::method1(int &a);

and are called like this:

int a;
class->method1(a);

But sometimes I see structures like:

void Class2::method2(int* a);

And these methods are called like this:

int a;
class2->method2(&a);

I understand that in the first case the method accepts an address of a variable, and in the second - pointer to a variable, right?

Could someone explain to me what is the difference between these two approaches, and when to use which?

Also, in the first example, it seems that a method can be taking "int& a" or "int a", and in both cases we would call it the same way: int a; class->method1(a); ? This seems confusing.

Thanks.

Monday to Friday
  • 239
  • 5
  • 16
  • Just to give you something to think about. What do you think could "int*&" or "int**" mean? Whats the difference and where could this be useful. – dowhilefor Jun 26 '13 at 14:43
  • in the second case `class2->method2(NULL);` is acceptable. That's the main difference. – andre Jun 26 '13 at 14:45

4 Answers4

1
void Class::method1(int &a);

This is passing by reference.

void Class2::method2(int* a);

This is passing the pointer. In class2->method2(&a);, & is the address of operater.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
0

In the first case, the method accept a reference to the variable. It means that if a is modified inside the method1, it will be modified after the function returns.

In the second case, it accepts a pointer to the variable. A pointer is the memory adress of the variable. It's what the & retrieves : The address (aka a pointer to a).

Both type have essentially the same purpose : Being able to modify a variable of a different scope.

See that example :

void fooPointer (int* pointer) {
    *pointer += 1;
}

void fooReference (int& reference) {
    reference += 1;
}

int main () {
    int a = 0;

    std::cout << a; // Ouputs 0

    fooPointer (&a);
    std::cout << a; // Outputs 1

    fooReference (a);
    std::cout << a; // Outputs 2
}

As you can see, both allow you to do the same here. But using references is usually easier and more readable, because everything is done implicitly, so you don't have to reference (*) or dereference (&) your variables.

Jerska
  • 11,722
  • 4
  • 35
  • 54
0

First one is called using a reference to a variable, second one - using a pointer. There are some difference between the two concepts. I encourage you to google this, but the most important ones I can think of right now are that a reference can not be constructed without pointing to an existing object and that a pointer can be NULL.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
0

void Class::method1(int &a) means you are passing a by reference and the caller can expect a to be modified.

void Class2::method2(int* a) means you are passing a by pointer and the caller can expect the thing to which a points to be modified.

Personally I don't like pass by reference since the caller doesn't quickly know if a will be modified as the calling syntax for pass by reference and pass by value are identical. Passing by constant reference void Class::method1(const int &a) is better still since then a cannot be modified and you can gain an efficiency in not taking value copies if a is a large object.

However many folk differ in their opinion and say that you should pass by pointer if the function allows the pointer to be null; i.e. can your function do something useful without a inputted?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483