C++ Notes to Self: Reference Variables

C/C++
‘C++ references allow you to create a second name for the a variable that you can use to read or modify the original data stored in that variable.’ [cprogramming.com]
Author

Dennis Chua

Published

February 5, 2017


In C++ a reference is an alternative name or an alias for a variable. It is created with an ampersand operator (&) and always needs to be initialized with a referent. The referent has to be a variable, not a literal. Once delcared this way, a reference is permanently bound to it referent variable.

Like a pointer, a reference can be used to access a variable. However, unlike a pointer, no new variable is allocated for a reference. Here are the differences between reference and pointer:

When it comes to functions and passing parameters by value, pointers and references offer the same semantics. (See the ptr_exchange() and ref_exchange() usage below.) But references have the advantage of clearer semantics. The absence of address and deference pointers removes the possibility of typo mistakes.

There is also the matter of null value parameters. With a pointer function parameter, we could pass a null adddress parameter, with the caveat that we face a possible access value exception during run time. If we tried to pass a null address (nullptr) to a function parameter that is a reference, the compiler will quickly flag this as an error.

#include <iostream>

using namespace std;
void ptr_exchange(int *a, int *b);
void ref_exchange(int &a, int &b);

int main(int argc, char **argv) {

    int x = 10;
    int &ref = x; 

    cout << &x << endl;
    cout << &ref << endl;
    cout << x << endl;
    cout << ref << endl;

    int a = 111;
    int b = 222;
    cout << a << "\t" << b << endl;
    ptr_exchange(&a, &b);
    cout << a << "\t" << b << endl;

    int w = 333;
    int z = 444;
    cout << w << "\t" << z << endl;
    ref_exchange(w, z);
    cout << w << "\t" << z << endl;

    return 0;
}

void ptr_exchange(int *a, int *b) {
    int temp = *b;
    *b = *a;
    *a = temp;
}

void ref_exchange(int &a, int &b) {
    int temp = b;
    b = a;
    a = temp;
}