Pointer operators in passing by reference

2

I've been studying some of the codes I found on the internet, and one of them used a prototype implementation as follows:

void changeMode(Mode &m){
    m.loop = true;
    m.quit = false;
}

See that the changeMode function is given a structure of type Mode . What I did not understand was the & operator. It also passes as argument as follows:

changeMode(player_mode);

I'd like to know why the & operator was used instead of the * operator. What changes from one to the other? So far, I know that & represents memory address, and * indicates a pointer. How could a function be receiving an address? Thank you.

    
asked by anonymous 12.06.2015 / 18:27

2 answers

3

The & operator, as well as several others in C ++, has more than one utility, depending on the context in which it is applied:

  • It can be used to get the address of a variable, the case you already knew.
  • It performs bitwise operation between two operands
  • It can also be used to declare parameters and variables of the reference type.

A reference is similar to a pointer, since it allows you to indirectly change values in an object. The differences are:

  • Since the reference is bound to one object it can not point to another
  • You access the reference as if it were a normal variable, without the need to use the * operator to derreference.
  • Except for rare cases, a reference never points to nullptr

It is very common to use const references to move from large objects parameter to functions, when you only want to observe them:

void teste(const Objeto &obj) { ... }

Already when you want to change the parameter, choose between references or pointers to parameters is almost a matter of style. The use of pointers may make it more explicit that the function will change the parameter, for example.

    
12.06.2015 / 18:50
2

Passing by reference (type &) is more "secure" than by pointer (type *), since it ensures that the object will never be null (*).

On the other hand, if you want to have the parameter option be null, then you are required to use pointer.

Pass an object as "const type &" is faster than passing the value because the object does not need to be copied. (Although I believe this is already optimized, but the theory is this).

An object passed or returned by reference does not need to use differentiated syntax (with ->) or rename pointers.

Retrieving a reference to an object is a very powerful construct, since it ensures that the returned object will never be null.

For "infix" operators, of type + =, - =, * =, etc. the correct way to implement is to return a non-constant reference to the "left" object:

tipo& operator+=(tipo& a, const tipo& b); // retorna ref para "a"

because these operators imply that the left object will be modified. For binary operators, the right is to return a new object:

tipo operator+(const tipo&a, const tipo&b);

(*) Yes, I know that there is always a way to pass null with casts, but the purpose of these C ++ features is to promote a higher level programming without casts.     

12.06.2015 / 19:25