//  (C) Porkolab 2003
//
//  A.3.1.
//   
//  The concept behind reference
//


// 1. allocates memory for an int type variable (in the stack)
// 2. binds the name "i" to this memory area.

int i;



// 1. allocates memory for an int type variable (in the heap)
// 2. no name has been bound to this memory

new int;



// 2. binds the name "j" to memory area already called "i"

int &j = i;




// History of reference in ALGOL68 language:

int i = 5;      // const int
ref int j := i; // int variable




// A memory area has <b>life</b>.
// A name has <b>scope</b>. So does a reference.


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



void f2()
{
    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