//  (C) Porkolab 2003
//
//  A.3.2.
//   
//  Value or reference
//


int f1()    // returns by value
{
    int i;  // local variable with automatic storage
    //...
    return i;   // return by value
}

int& f2()    // returns by reference
{
    int i;  // local variable with automatic storage
    //...
    return i;   // returns the reference
}

int j = f1();   // ok: value of i has copied into j
int& j = f2();   // bad: no copy, j refers to invalid place




// real-life examples:

    // returns reference to returning the original object
    date& date::set_year(int y) { year=y; return *this; }


    // returns reference to returning the original (incremented) object
    date& date::operator++() { next(); return *this; }


    // returns value with copy of the temporary object before incrementation
    date  date::operator++(int) { date curr(*this); next(); return *curr; }





// a key diffeernce between pointer an reference: null pointer

    Base *bp = ...;

    // null, if dynamic_cast is invalid
    if ( Derived *dp = dynamic_cast<Derived*>(bp) )
    {
        // ...
    }


    Base &br = ...;

    // throws exception, if dynamic_cast is invalid
    try
    {
        Derived &dr = dynamic_cast<Derived&>(br);
        // ....
    }
    catch( bad_cast ) { ... }