2

I'm coming from C and I've learnt that if we pass an argument to a function it'll only make a copy of it with it's value so if we change this variable value the variable that was passed to the function won't change because they're obviously 2 different variables with 2 different addresses just with the same value. So in C if we would want to change a parameter that is being passed to a function we would pass a pointer with the parameter address.

Now, in c++ we could do the same, but we could also do this :

void changeValue(int& n)
{
    n = 10;
}

Not only that we're not de-referencing to assign a value but when we pass the variable to the function we pass it as is without the address (&) operator. So I don't understand what is the difference here ? If we don't de-reference is it even a pointer ? Also if we pass it as is without the & operator how is it that we pass it by reference ? Could someone clear some things for me ?

Tugal.44
  • 153
  • 4
  • 13
  • What is the difference with what? The function signature says the parameter is passed by reference, so it is. – juanchopanza Sep 22 '14 at 15:14
  • 1
    does this thread helps? http://stackoverflow.com/questions/410593/pass-by-reference-value-in-c – taocp Sep 22 '14 at 15:14
  • The simple answer is that 'C' only has pass-by-value, while C++ has pass-by-value and pass-by-reference. So you should learn the latter of the two, since this concept does not exist in 'C'. Also, trying to learn C++ by using 'C' as a guide -- that may work for some things, but if the concept doesn't exist in 'C' at all (such as pass-by-reference), then you'll just get confused, as is the case now. – PaulMcKenzie Sep 22 '14 at 15:26
  • @taocp basically the variable in the function has the same address as the variable passed to the function and yet it's not a pointer right ? just 2 variables that share the same address ? – Tugal.44 Sep 22 '14 at 15:33
  • @Tugal.44 since reference is just an alias to an existing variable, so my answer is yes – taocp Sep 22 '14 at 15:34
  • @taocp I see thanks for explaining. Is there any reason then to pass pointers and not by reference in C++ unlike C ? – Tugal.44 Sep 22 '14 at 15:45
  • @Tugal.44 - `Is there any reason then to pass pointers and not by reference in C++ unlike C ` If the parameter passed is optional, then usually a pointer is preferred, since you can make it NULL which would denote "don't use me". If the parameter is not optional, then you usually pass a reference, since NULL's and references do not play well with each other. See this link for further info: http://stackoverflow.com/questions/4364536/c-null-reference – PaulMcKenzie Sep 22 '14 at 17:07

1 Answers1

3

C++ has pointers (to maintain backwards compatibility with C) and references. With pointers, as you mentioned, you are dealing with addresses, dereferencing operators, etc. With C++ references, you don't have to worry about that because the reference is an alias to the other variable. That is, any modification made to the reference will be reflected in the actual variable.

References can be implemented by the compiler as pointers, but it's not required to.

References really shine when you want to overload operators. For example, if you construct a custom numeric type to represent a matrix, you can create an addition operator such as

matrix operator + (matrix m1, matrix m2);

But that would incur a lot of copying. You can use pointers:

matrix operator + (matrix* m1, matrix* m2);

but then calling the code would be really ugly:

matrix m = &m1 + &m2;

References allow us to write the operator in a way that avoids copying, and allows us to cleanly use it at the call-site without reaching for the address-of operator.

So in conclusion: references in C++ are similar to pointers in that they allow you to refer to the same value through multiple variables. However, unlike pointers, which require the use of memory addresses, references are a syntactic convenience, who's actual implementation is up to the compiler.

I hope this helps.

bstamour
  • 7,746
  • 1
  • 26
  • 39
  • basically the variable in the function has the same address as the variable passed to the function and yet it's not a pointer right ? just 2 variables that share the same address ? @bstamour – Tugal.44 Sep 22 '14 at 15:32
  • You can think of it that way, yes. If you want to use pointers to ease your understanding, a reference is a constant pointer (i.e. one that cannot be changed to point elsewhere.) – bstamour Sep 23 '14 at 12:44