Reference to Variables?
-
What is the advantage of using a reference over the variable. Can't you just pass the variable name instead of passing a reference to the variable.:confused: KAI_YPO
-
What is the advantage of using a reference over the variable. Can't you just pass the variable name instead of passing a reference to the variable.:confused: KAI_YPO
When you pass a variable, a copy of that variable is made. When you pass a reference, the original variable is passed in. This means two things: 1. If the variable is big, you save some effort in making a copy 2. Anything you do to the variable in your function will effect the variable that was passed in so if you have two functions: void Inc(int i) { ++i; } and void IncRef(int & i) { ++i; } In your main code you can do this: int i = 0; Inc(i); // i still = 0, because only the i in the function was incremented IncRef(i); // i now = 1, as this i is the same one that was incremented in the function. Note, the variable names do not need to be the same. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
-
When you pass a variable, a copy of that variable is made. When you pass a reference, the original variable is passed in. This means two things: 1. If the variable is big, you save some effort in making a copy 2. Anything you do to the variable in your function will effect the variable that was passed in so if you have two functions: void Inc(int i) { ++i; } and void IncRef(int & i) { ++i; } In your main code you can do this: int i = 0; Inc(i); // i still = 0, because only the i in the function was incremented IncRef(i); // i now = 1, as this i is the same one that was incremented in the function. Note, the variable names do not need to be the same. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
Here's a question though... Which is more efficient... void IncRef(int & i) { ++i; } or this... void IncRef(int i) { ++i; } The way I think about it is we are still passing pointer to another variable in a previous stack frame. Therefore in this case would it make any difference performance wise since we are still just dealing with a number whether it be a memory location or an integer. I know that if this was a pointer I would be correct in this case but I'm not sure about references. Please feedback though I may be wrong.
-
Here's a question though... Which is more efficient... void IncRef(int & i) { ++i; } or this... void IncRef(int i) { ++i; } The way I think about it is we are still passing pointer to another variable in a previous stack frame. Therefore in this case would it make any difference performance wise since we are still just dealing with a number whether it be a memory location or an integer. I know that if this was a pointer I would be correct in this case but I'm not sure about references. Please feedback though I may be wrong.
In this case, it would only be barely more efficient to pass a reference. It's not just passing a number, it's also allocating and deallocating the memory. But your app won't hang on this, it's more if you have huge data structures that you want to pass into a function to be read ( and this is what keyword 'const' is for ), that you can make some serious gains in efficiency by using references. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder