I don't know if this is the right or wrong. Allow me to explain.
So I paused the video when the Chili first showed the Swap() function not working and thought about it. Coming from a highlevel scripting language I already know a method to fix it so I tried it and it worked.
Instead of adding a bunch of pointer references, I just referenced the input arguments. 2 ampersands to fix the code:
Code: Select all
void Swap(int &x1, int &x2){
int temp = x1;
x1 = x2;
x2 = temp;
}
Code: Select all
Swap(x1, x2)
My question to the pros is, why /not/ use this method? Is there a disadvantage? Thanks.