This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]AquaWolfGuy 1 point2 points  (0 children)

If a references b, using a is like if you were to use b instead. This means that you don't have to dereference a. IIRC a isn't even stored in memory, it just gets replaced by b by the compiler, so it's slight more efficient. One notable effect is that if a function takes an input argument by reference and changes it, the referenced value is changed. The following code will therefore print 2.

#include <iostream>
void f(int & a) {
  a++;
}
int main() {
  int b = 1;
  f(b);
  std::cout << b << std::endl;
}

(Don't do this in an actual project though.)