As someone who likes working with higher level languages, I never understood the pass by reference or even referencing different pointers. It never stuck out to me as useful in what I want software to do. It’s too close to hardware.
if you’re working with higher level languages you pass-by-reference all the time. give a list to a function to modify it? that’s by reference. giving an event handler function to a framework? that’s by reference. setting a property on an object? that’s usually by reference.
Your example doesn’t make sense to me. Why would you modify the entire list by checking if something is in it? Also, you can totally edit the list via a pointer, that’s how you’re supposed to edit the list if you want any performance. Otherwise you’d be copying the list on every modification, which is terribly inefficient
Now that explain the & part of the pointers that I never really understood.
The
&operator references the value.In C++, the
&at the function argument makes it a reference type (safe pointer).void someFunction(int& refVal) { [...] }As someone who likes working with higher level languages, I never understood the pass by reference or even referencing different pointers. It never stuck out to me as useful in what I want software to do. It’s too close to hardware.
if you’re working with higher level languages you pass-by-reference all the time. give a list to a function to modify it? that’s by reference. giving an event handler function to a framework? that’s by reference. setting a property on an object? that’s usually by reference.
the list is the helpful part to understanding it.
it would be terrible if, with bar being a list and foo being a member of the list
modified the list. So yeah, you want to look at the list not edit the list, it’s a pointer.
other way round surely? if you want to modify the original object, use a pointer. if you don’t, use a copy.
Your example doesn’t make sense to me. Why would you modify the entire list by checking if something is in it? Also, you can totally edit the list via a pointer, that’s how you’re supposed to edit the list if you want any performance. Otherwise you’d be copying the list on every modification, which is terribly inefficient
Reference values are quite useful, such as:
*x = dereference or “point to”. Treats the variable
xas containing a pointer value. Evaluates to a variable existing at the address inx.&x = reference or “get address of”. Evaluates to the address of
x.They’re complimentary operators, so
*(&x)cancels out and is equvalent to justx.