Pointers/Memory Allocation   «Prev  Next»
Lesson 4 Reference declarations
Objective Create aliases for variables using reference declarations.

C++ Reference Declarations

C++20 does offer several mechanisms that can effectively serve this purpose:
  1. References (since C++98):
    • Declare a reference using the `&` symbol:
        int x = 10;
        int& y = x;  // y is a reference to x
        y = 20;  // Modifies x through the reference y
        
    • Key points:
      1. Must be initialized upon declaration.
      2. Cannot be reassigned to refer to a different variable later.
      3. Effectively create an alias for the original variable.
  2. Type Aliases (since C++11):
    • Define a type alias using `using`:
        using IntAlias = int;
        IntAlias z = 5;  // z is an integer
        
    • Not aliases for specific variables, but create alternative names for types.
  3. Structured Bindings (since C++17):
    • Decompose objects or tuples into separate variables:
        std::pair p = {10, 3.14};
        auto [x, y] = p;  // x is an int, y is a double
        
    • Can be used to create aliases for members of structs or classes.
  4. `std::reference_wrapper` (since C++11):
    • Wrap a reference in a class object:
        int a = 42;
        std::reference_wrapper b = a;
        b.get() = 50;  // Modifies a through the reference wrapper
        
    • Useful for storing references in containers or passing them to functions that expect objects.
  5. 5. Pointers:
    • While not strictly aliases, pointers can provide indirect access to variables:
        int num = 25;
        int* ptr = #  // ptr points to num
        *ptr = 30;  // Modifies num through the pointer
        
Choose the appropriate mechanism based on your specific needs, considering factors like:
  • Whether you need to modify the original variable through the alias.
  • Whether you need to store the alias in a container or pass it to a function.
  • Whether you want to create an alias for a specific variable or for a type in general.


C++ allows reference declarations[1] so you can create aliases for variables. These declarations are typically of the form:
type& identifier = variable

Example

Here are two examples of reference declarations:
int  n;
int&  nn = n;

double  a[10];
double& last = a[9];

The name nn is an alias for n. They refer to the same place in memory, and modifying the value of nn is equivalent to modifying n and vice versa.
The name last is an alternative way of referring to the single array element a[9]. An alias, once declared, cannot be changed. In other words, if you declared last as an alias for the array element a[9], you could not also declare last as an alias for the array element a[1].
Reference declarations that are definitions must be initialized, usually as simple variables. The initializer is the lvalue expression, which gives the variable's location in memory. When a variable i is declared, it has an address and memory location associated with it. When a reference variable r is declared and initialized to i, it is identical to i. It does not have a separate identity from the i.

[1]: Reference declarations declare the identifier to be an alternative name or alias for a variable specified in an initialization of the reference.

SEMrush Software