what does & in this code do?

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....
1
2
  Void Snake::Moveby( const Location& delta_loc );
Last edited on
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.

If you don't know what these are, this might help.
https://www.cs.fsu.edu/~myers/c++/notes/references.html
so does this mean that delta_loc is beind passed by reference to Location?
Lets say you have code like:
1
2
3
4
Snake foo;
Location bar;

foo.Moveby( bar );

There is a function call. During this function call, in the body of the function, the delta_loc is a reference to object bar.
Hello programmy,

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.

Hope that helps,

Andy
kbws point that C does not pass by reference.

That is correct. C does not and did not have a reference type like C++ has.
https://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29


A third use of "&" is operator&, which is bitwise AND.


C++11 did add:
Rvalue reference declarator: the declaration S&& D; declares D as an rvalue reference to the type determined by decl-specifier-seq S.
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);


And note that that pointer is passed by value.

@jlb,

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.

Thank you for the correction.

Andy
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

using namespace 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---
Last edited on
void Snake::Moveby( const Location& delta_loc );

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.
Ok, but now I have a question for everyone...

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.

So this is a memory saving trick... Thanks jlb that is good to know.
So this is a memory saving trick...


That depends on the class. It can be either a memory saving trick or a time saving trick, or both.

thanks for shedding light on this! guys :)
Topic archived. No new replies allowed.