HOW TO RETURN A REFERENCE IN C ?

Tanuj Kumar

Total Post:134

Points:940
Posted by  Tanuj Kumar
 953  View(s)
Ratings:
Rate this:
Hi Expert,

How to return a reference in c++?

Thanks in advance. 
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: How to return a reference in c ?

    A function can return a reference.Returning a reference can be very useful when you are overloading certain types of operators.
    However,it also can be employed to allow a function to be  used on the left side of an assignment statement.

    e.g.

    int &f( )//return a reference
    int x;
    int main( )
    {
    f( )=100; //assigning 100 to reference returned by f()
    cout<< x <<"\n";
    return 0;
    }
    //return an int reference.
    int &f()
    {
    return x;  //return a reference to x
    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!