C++ introduces the reference operator, &, which works slightly different than it does in C.

For those who come from a C background and understand pointers, treat it as though double &b = a goes to double *b = &a and all the subsequent usage of b is replaced with *b. Note that the double that b points to cannot be changed! This is unlike a normal pointer, double *b = &a, in where you could later write b = &c. This will not work with a reference.

What Are They Good For?

References can simplify the syntax of certain pointer manipulation. They also useful when creating operator overloads, (e.g. the ++ operator for an enumeration), so that the syntax remains consistent with the base types in C++.

Undefined behaviour occurs if you ever return a local variable by reference, even if it’s an rvalue reference.

Posted: December 25th, 2013 at 3:17 pm
Last Updated on: June 11th, 2017 at 12:27 pm