Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
I have a question about references in C++. From what I understand it's like a pointer but with a few differences. It can't be null, it can't be re-assigned and it must be initialized. Apart from this it behaves pretty much as a pointer. However I have seen some examples when I have been searching the web to find answers, that showed that references doesn't really behave as pointers after all.

In the example below I expected the reference 'n1' in the function to be 10 since it's a reference to the variable 'n1' in main. If it's a reference how can it then still be 1 even if the value is changed. It must be a copy somewhere since obviously both 1 and 10 exists at the same time. For me it acts more like a copy of the value than a reference.

C++
#include <functional>
#include <iostream>
 
void f(int& n1, int& n2, const int& n3)
{
    std::cout << "In function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
    ++n1; // increments the copy of n1 stored in the function object
    ++n2; // increments the main()'s n2
    // ++n3; // compile error
}
 
int main()
{
    int n1 = 1, n2 = 2, n3 = 3;
    std::function<void()> bound_f = std::bind(f, n1, std::ref(n2), std::cref(n3));
    n1 = 10;
    n2 = 11;
    n3 = 12;
    std::cout << "Before function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
    bound_f();
    std::cout << "After function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
}

Output:
Before function: 10 11 12
In function: 1 11 12
After function: 10 12 12


I would be thankful if someone of you that know could help me understand what a reference in c++ really is and how it's stored in memory. (I'm not new to programming but lack some experience and knowledge in c++).

The code is taken from: http://en.cppreference.com/w/cpp/utility/functional/ref[^]

The problem is solved, I didn't realized that bind actually takes a value and therefore n1 is only a temporary variable in the function. The notation was a bit confusing though I think. Anyway thanks for all help!
Posted
Updated 17-Jun-13 12:08pm
v3

Ok, I think you may have confusion about these two things:

& and *

& does not mean "reference" it means Address Of, which passes the address of the item on the right side. When passed with &, the values are passed by reference which means you can modify the values and they are reflected up the call stack.

* is the dereference operator, which returns the value of the item. Passing by value means you can do whatever you want in the function but its value is not reflected back up the call stack (the value reverts back to the original when the function exits).

*EDIT*

I may have been too quick to answer, see this[^] to see what the std::ref and std::cref really do and how it works.

I see you got the code from here[^]
 
Share this answer
 
v3
Comments
WaZoX 17-Jun-13 15:44pm    
My question is about the build-in references in C++. I understand std::ref() I think. =)
Sergey Alexandrovich Kryukov 17-Jun-13 17:18pm    
It looks like a good unconfusion, but OP wasn't probably unfconfused. Anyway a 5.
—SA
As bind function argument, n1 is not a reference (it is passed by value) hence f gets a reference of a temporary variable.
 
Share this answer
 
Comments
WaZoX 17-Jun-13 17:46pm    
That explains a lot =) Thank you very much! 5.
CPallini 18-Jun-13 1:44am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900