I know following about reference
eg. int &ref=x;
then ref and x are the names of same locations in memory.
Memory is not allocated for reference unlike pointers.
I was writting a simple swap program in C++ using references which I successfully wrote.
Then i decided to try out what happens when a function which returns reference is LHS of an expression and now I am unable to predict the output of the below code.
#include<iostream.h>
int & swap(int &,int &); //function returns reference
void main()
{
int x=10,y=20;
int &ref1=x,&ref2=y;
swap(ref1,ref2)=x; //what happens here?
cout<<x<<y;
}
int & swap(int &ref1,int &ref2)
{
int temp;
temp=ref1; //swap
ref1=ref2; //code
ref2=temp; //here
return ref2; //tried out this(has nothing to do with swapping)
}
O/P 20 20