I am trying to associate a C ++ link with a pointer

Before saying that this will be a repeating question and a downward one (as it was before), I searched and found nothing.
I, like many others, are trying to study the use of C ++ reference variables and associate them with pointers. It was easier for me to make the table, and I need to know if it needs to be fixed.

                   int *n   int n    int &n    caller/local
void foo(int *n)     n       &n        &n          caller
void foo(int n)     *n        n         n           local
void foo(int &n)    *n        n         n          caller

The table wants to reflect all the parameters accepted by law.

[1,1]: passing by reference (trivial)  
[1,2]: passing by reference  
[1,3(1)]: passing by reference, an is an address(?)  
[1,3(2)]: passing by reference, as n is used as alias(?)  
[2,1]: passing by value, as dereferencing  
[2,2]: passing by value (trivial)  
[2,3(1)]: passing by value, using value of n (where n is an alias)  
[2,3(2)]: passing by value (dereferencing n, which is an address)  
[3,1(1)]: passing by reference, as foo accepts address  
[3,1(2)]: passing by reference, reference of value at address n  
[3,2(1)]: passing by reference (trivial)  
[3,2(2)]: passing by reference, as foo accepts address or reference  
[3,3]: passing by reference (trivial, as argument matches parameter exactly)  
  • Are the tables and explanations correct?
  • Are there any cases left outside the table (other than derivatives such as * & n, pointer to pointer, etc.)?
+4
source share
3 answers

Function

void foo(int& n);

does not accept an address (pointer), not literals.

,

int a = ...;
foo(&a);  // Trying to pass a pointer to a function not taking a pointer

foo(1);  // Passing R-value is not allowed, you can't have a reference to a literal value

, , ,

int foo(const int& n);

, .


void foo(int* n);

.

, :

int a = ...;
int& ra = a;   // ra references a

foo(&a);  // OK
foo(&ra); // OK
foo(a);   // Fail, a is not a pointer
foo(ra);  // Fail, ra is not a pointer
foo(1);   // Fail, the literal 1 is not a pointer

:

void foo(int n);

:

int a = ...;
int& ra = a;   // ra references a
int* pa = &a;  // pa points to a

foo(a);   // OK, the value of a is copied
foo(ra);  // OK, the value of the referenced variable is copied
foo(*pa); // OK, dereferences the pointer, and the value is copied
foo(pa);  // Fail, passing a pointer to a function not expecting a pointer
foo(1);   // OK, the literal value 1 is copied
+5

operator* operator&

operator& () operator* () .

, n type, *n , , type*, type&, type.

(, ):

struct type {
    int x;
};

int& operator&(type& t) {
    return t.x;
}

Live demo

. *n, **n, ***n type*, type&.

:

void func_ptr(int*) {}
void func_ref(int&) {}

:

int a;
int* b = &a;
int** c = &b;
int*** d = &c;

:

func_ptr(b);
func_ptr(*c);
func_ptr(**d);

func_ref(*b);
func_ref(**c);
func_ref(***d);

Live demo

? - , ( , * & n, ..)?

, .

+1

, , , int* n, n int, n :

                   int *n   int n   int &n
void foo(int *n)     n       &n      n/&n
void foo(int n)     *n       n      n/*n
void foo(int &n)    n/*n     n/&n      n

, :

foo(int* n):

  • int *n &n
  • int& *n

foo(int n)

  • n, &n *n
  • int& n n, *n
  • , n , n

foo(int& n):

  • n, &n n *n
  • n n / &n

, ( n <<21 > - n )

                    int*   int    int& n2 = ...    int* p = ...   caller/local
void foo(int* p_n)   p_n   *p_n      *p_n            p_n             caller
void foo(int n)      &n     n         n              &n              local
void foo(int& n)     &n     n         n              &n              caller

, .

UPDATE: your comment explains that you want the table to be given a “submitting form,” which I think is the code you need in the caller foo. The correct table for this should be:

                   int *n   int n   int &n    caller/local
void foo(int *n)     n       &n      &n          caller
void foo(int n)     *n       n       n           local
void foo(int &n)    *n       n       n           caller
+1
source

Source: https://habr.com/ru/post/1546291/


All Articles