0
What is an argument reference
plis help
2 ответов
+ 1
Do you know about pointers ?
0
A pointer is a variable wich store an adress of another variable.
An adress of a variable is where it is in the memory.
int a=5;
cout<<a; // value of a : 5
cout<<&a; // adress of a : something like 0x3f46gh
With & operator, you can have the adress of any variable.
Now about your question :
int swap(int c, int d){
int x=c;
c=d;
d=x;
cout<<c<<d; // output : 21
}
int main(){
int a=1, b=2;
swap (a, b);
cout<<a<<b; // output : 12
}
When swap() is executed, c and d are COPIES of a and b. So c and d are swapped. Not a and b.
But if swap() was :
void swap(int& c, int& d)
The adress of a and b would be sent to swap.
Then, every changes on c, would apply to a.
The argument reference is this case.
If you don't understand ask !