C++ PROGRAMMING
References
Article by:Geoffrey Hunter
Date Published: | |
Last Modified: |
Overview
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.
Authors

This work is licensed under a Creative Commons Attribution 4.0 International License .