Scope and Life Rules

A normal variable has scope and life, a reference has only scope.


void f1()
{
    int i;  // start of scope and life i

    int &ir = i;    // start of scope ir, ir bound to i

    ir = 5; // ok

}   // end of life i, end of scope i and ir

And this can lead to problems:


{
    int *ip = new int;  // start of life *ip

    int &ir = *ip;  // start of scope ir, ir bound to *ip

    delete ip;  // end of life *ip here

    ir = 5; // bad

}   // end of scope ir