Help with reference "&"

Hi,

I'm learning C++ by my own with book "Programming:Principles and practice using c++" (I know something about C).

I can't understand the meaning of "&" in cases like "type& variable".

For exemple: int& a or void f(int& a) or int& f(int& a).

Because if I didn't put & the result is the same right?

Thanks!
it's kinda like an 'alias' to the variable you are referencing.
See here:
http://cplus.about.com/od/learning1/ss/references.htm
No they're different. See this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>

/* Couple of simple incrementer functions. One BV, one BR. */
void incrementByValue( int n ) { n++; }
void incrementByRef( int &n ){ n++; };

int main( int argc, char* argv[] )
{
    /* Reference example */

    /* An integer */
    int a = 10;     

    /* A reference to an int. Note, this has to be initialised when declared */
    int &a_ref = a;  

    /* Print our values. They'll be the same */
    std::cout << "a is " << a << std::endl;
    std::cout << "a_ref is " << a_ref << std::endl;


    /* Change a */
    a = 5;

    /* Print our values again. Note a_ref changes because a has */
    std::cout << "a is " << a << std::endl;
    std::cout << "a_ref is " << a_ref << std::endl;

    /* Call our by value function */
    incrementByValue( a );

    /* Print a. Note that it's still five. 
       This is because when we pass an argument to a function, a local copy
       of that variable is made. So within the BV function, the variable 'n'
       is not the same variable as 'a'. It is a copy that is discarded when
       the scope of the function ends */
    std::cout << "a is " << a << std::endl;

    /* Let's call our by reference function */
    incrementByRef( a );

    /* Print a. Note that it's changed.
       Because we pass by reference, we're not creating a copy in the function,
       but rather we're using the instance 'a' that we created in main.
       Therefore, any changes within the function will affect 'a'. */
    std::cout << "a is " << a << std::endl;

    return 0;
}


Your latter example, int &f (int &a) is bad in my books. Others may disagree, but I'd strongly recommend that you don't return references from functions. For example, if you're returning a reference to an object that is local to the function, the reference is referring to nothing as the variable is destroyed when you leave the scope of the function.

Ignore that, I misread the code as a function prototype. It's not.
Last edited on
i'm assuming you know about funtions

1
2
3
4
5
6
7
8
9
10
int multiply(int a, int b)
{
      int c = a*b;
      return c;
}

int main()
{
     int num = multiply(2, 4);
}


makes a copy of the a and b.

which in this case is fine. however if we were using a variable for them in a different program:

1
2
3
4
5
6
7
8
9
10
11
int add_one(int num)
{
     num++;
     return num;
}

int main()
{
     int num = 0;
     num = add_one(num);
}


you would need to reassign the variable after adding the one.

however if you referenced it using & you wouldn't need to:

1
2
3
4
5
6
7
8
9
10
void add_one(int &num)
{
     num++;
}

int main()
{
    int num = 0;
    add_one(num);
}


would produce the same result without temporarily copying the variable.

in this program it wouldn't matter but if you were doing it with a class. it is better to reference it then make a copy of it.


forgive me if i made an error
Topic archived. No new replies allowed.