Home > DeveloperSection > Forums > How to return a reference in c ?
Tanuj Kumar

Total Post:134

Points:940
Posted on    June-14-2013 9:43 AM

 Visual C++ Visual C++ 
Ratings:


 1 Reply(s)
 692  View(s)
Rate this:
Hi Expert,

How to return a reference in c++?

Thanks in advance. 


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    June-15-2013 4:50 AM

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
}

Don't want to miss updates? Please click the below button!

Follow MindStick