void foo(int& f){ cout << f << endl; } void foo1(int& f){ cout << *&f << endl; } 

Are there any differences in these records? They derive the same value. But maybe the difference is somewhere deeper? And also in this case:

 void foo(int*& f){/* ... */} void foo1(int& f){/* ... */} 

Records will be equivalent, not taking into account that you have to dereference f to get the value? What to use better? I understand that it is better to use the second function if we pass variables, and the first one when working with an array. Correct, if not right.

  • In this example, the entry *&f meaningless. You can hang more paired taking of addresses and dereferencing, but why? - VTT
  • Can you give an example when such a syntax makes sense? And does it even have? - emoxypine
  • You can come up with an artificial example when the operators & and * overloaded, but something realistic is what I find it difficult ... - VTT
  • In the second paragraph, the entries are not equivalent at all. int x; foo1(&x); compiles, but foo(&x) does not (in order for foo work, you must first write the address to a variable). - HolyBlackCat 2:07 pm
  • "it is better to use the second function if we transfer variables, and the first one when working with an array"? It is not clear what is meant. Give an example. int a[10]; foo(a); - it won't even compile. What kind of "work with an array" are we talking about? - AnT

2 answers 2

If we are talking about a reference to a fundamental type, such as int &f , then there is no difference between f and *&f . There is no difference not only in terms of the observed value, but also in terms of the observed object . But if you replace the int type with a class-type, then due to operator overloading, the difference can be arbitrarily large. It is not clear from your question whether it is associated with the int type or not.

What kind of "equivalence" you ask in the second case is not clear, since the parameters of the function refer to completely different entities. There is no logic in the statement “it is better to use the second function if we transfer variables, and the first one when working with an array” I do not see.

     void foo(int*& f) 

    here the argument is a non-constant reference to the pointer, so you can perform actions on the pointer in the function body, and then the changes will occur with the pointer that you pass:

     { ++f; } 

    But you cannot transfer the name of an array to a function, because the name of the array is associated with a certain number of elements, and therefore it can be considered as a constant link. Those. In the following function:

     void foo1(int* const& f) { cout << *(f + 1) << " : " << *f << endl; } 

    you can pass the name of the array. The type of the argument does not allow performing actions with the pointer itself, and (f + 1) is another pointer.

    In the following function:

     void foo2(int* f){ ++f; } 

    You can pass both the array name and any pointer, since the argument is a different pointer, initialized with the same address. (copying takes place)

    And finally:

     void foo3(int& f) { ++f; } 

    It has an argument_of the object. Those. The function can be passed to the object, and all changes will occur with this object. In this case, the value of the object is incremented.

    In order to better understand all this, write a test program for yourself. For example:

     int a[] = {10, 20, 30}; int *m = a; foo2(m); cout << *m << " ... " << *a << endl; foo1(a); cout << *m << " ... " << *a << endl; foo(m); foo(a) // error foo2(a); cout << *m << " ... " << *a << endl; foo3(a[1]); cout << *m << " ... " << *a << endl;