I tried looking this up in other websites but it only
turns up as pass by reference with the & beside variables,
the & has several meanings but one has made me boggled
what does the & in this code mean?and can anyone give me an
example of how it is used? i believe it's called
pass an argument by reference to a function but I'm confused
as to how it's used, the site it was located on was vague
in describing it....
I tried looking this up in other websites but it only turns up as pass by reference
And that is correct.
There are different ways in which parameters can be passed to functions/procedures in general. C has only pass by value, C++ has pass by value and pass by reference.
All above answers are correct, but I beg to differ with kbws point that C does not pass by reference. Many years ago I once had a C program that used pass by reference. So unless the C99 standard has or the C11 standard has changed this C should still be able to pass by reference.
In your example Void Snake::Moveby( const Location& delta_loc ); I can tell this is pass by reference by the use of the "&". I believe this is the most acceptable way writing pass by reference, but I have seen Void Snake::Moveby( const Location & delta_loc ); with a space on either side of the "&".
The other use of "&" is the address of as in int* add = & num;. This puts the address of "num" into the pointer "add".
I looked at kbw's link and it looks like good reading.
Many years ago I once had a C program that used pass by reference.
In C "passing by reference" means passing a pointer that points to the address of the object.
1 2 3 4 5 6 7 8 9 10 11
void some_function(int *some_parameter)
{
// assign a value to the parameter.
*some_parameter = 1000;
}
...
int value;
// Pass the address of value into the function.
some_function(&value);
Nor exactly what I remember, but I did not stay with C as I would have liked to. I wish I still had that program because it would be interesting to look at.
c++11 onwards is so amazing, though! (even if VS2012 didn't fully support the spec)
@programmy - also note that your example showcases not just a reference, but a const reference. The function is basically saying, "Hey Caller, don't worry -- I won't modify your delta_loc". I think you also meant to lowercase void; prob just a typo.
Edit: explicit example showing what jlb said (that a copy of the pointer is made)
#include <iostream>
#include <string>
usingnamespace std;
void Fun(string* p)
{
cout << "(Fun) address of string "<< p << " from pointer " << &p << endl;
*p += "---";
}
int main()
{
string s("La la la");
string* ps = &s;
cout << "(main) address of string "<< ps << " from pointer " << &ps << endl;
cout << s << endl;
Fun(ps);
cout << s << endl;
return 0;
}
Possible output:
(main) address of string 0x7ffe67178040 from pointer 0x7ffe67178038
La la la
(Fun) address of string 0x7ffe67178040 from pointer 0x7ffe67178018
La la la---
This is a void function called Snake::Moveby. Being void it returns nothing, but it takes as an argument a const Location. We will call this const Location delta_loc, and instead of passing the value of delta_loc, we will take the address of delta_loc. This is what the & operator does here.
Note this example...
1 2
int x = 3;
std::cout << &x;
The displayed result will be the address of x and not 3.
When we pass by reference, it is so we can change the value of a variable from inside the function, right? But a const can not be modified. Why pass it by reference then?
When passing by const reference you avoid the copying of the parameter that happens with pass by value. When dealing with non-trivial classes this copying can be quite expensive.