Pointer operators passing by reference

Asked

Viewed 704 times

2

I’ve been studying some 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 function changeMode receives a type structure as a parameter Mode. What I didn’t understand was the operator &. It also passes as argument as follows:

changeMode(player_mode);

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

2 answers

3


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

  • It can be used to get the address of a variable, the case you already knew.
  • It makes the operation "and" bit by bit 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:

  • Once the reference is linked to an object it cannot point to another
  • You access the reference as if it were a normal variable, with no need to use the operator * to undermine.
  • Except in rare cases, a reference never points to nullptr

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

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

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

2

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

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

Passing an object as "const type&" is faster than passing the value, as 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 need not use differentiated syntax (with ->) or dereference pointers.

Resuming a reference to an object is a very powerful construction, as 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 there’s always a way to pass null with Rfsts, but the goals of these C++ features is precisely to promote higher-level programming without Rfsts.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.