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

Total Post:134

Posted on    June-14-2013 9:43 AM

 Visual C++ Visual C++ 

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

How to return a reference in c++?

Thanks in advance. 


Total Post:604

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.


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