Hello, please tell me how it works? Suppose there is such a function:

void foo(int &one, int two){ one = two; } int s1 = 1; int s2 = 2; foo(s1, s2); 

How does an ampersand work before the parameter of the one function? I understand that the function works with a variable that is allocated in static memory. It turns out that we pass a value of type int to the function, then we take its address. But why in the body of a function can we work with it as with a normal variable, and not as with a pointer? In theory, it would be necessary to dereference.

This design is clear:

 void foo(int* one, int two) { *one = two; } 

and when the function is called, we pass the parameters (&s1, s2); .

  • So under the hood, count the pointer is transferred as a result. - Vladimir Gamalyan

2 answers 2

Therefore, we write & so that you can write one = two , not *one = two . The whole point here is that the pointer is obviously passed when they want to work directly with it, when it is necessary to know its meaning. When we want to work with the value of a variable, but not only in the function in which it is created, but also in “hereditary”, then we pass it by reference.

Of course, you can say something like “I am not used to it, I would rather send the addresses explicitly,” but believe me - passing a variable by reference in case you need to work with the variable itself is the right approach to programming. A good programmer must be able to write a GOOD code - not only algorithmically efficient, but also as clear as possible; such that its essence was clear to another programmer, even to the first person who saw it.

In C #, passing a variable by reference looks more beautiful, it simply writes the word ref (or out in front of the calling method and the variable in front of the variable (or out , which means “additional return value”).

  • one
    @Skripkin90, passing a parameter by reference is a very unfortunate design solution in crosses, often making it difficult to find errors (in other people's programs). If the parameter can change in the called function, then it is better to pass the address explicitly. - No wonder you gave an example with C # (ref in both methods). - avp
  • aa, you mean that by debugging someone else's codes, you pass a function mistakenly believing that the parameter in it is passed by copying, and there actually the parameter is passed by reference, and ... well, in general, you lose the thread when debugging? Yeah, you're right, it really can make it very difficult to debug someone else's code ... Yes. I just write on sharpe. - Skripkin90
  • @avp but const Foo& foo will allow not to check for nullptr , with const Foo* foo would not be so. - Vladimir Gamalyan

read the code below, I think you will understand

 #include <iostream> using namespace std; int main(){ int value = 0; // переменная инициализированная нулем cout << value << endl; // выведет нуль int & link = value; // ссылка на переменную (не может существовать без указания имени переменной) link = 100; // через ссылку записываем в переменную значение 100 cout << value << endl; // выведет 100 return 0; } 

the same happens when passing a reference to a function. link is just the second name of the variable (psefdonym)