C ++: Unable to convert from foo & to foo *

I have a method:

odp(foo& bar);

I am trying to call it:

foo baz;
odp(&baz);

I get a compiler error:

error C2664: "odp" cannot convert parameter 1 from 'foo *' to 'foo &'

What am I doing wrong? Am I passing a link to baz?

UPDATE . Perhaps I have a misconception about the relationship between pointers and links. I thought they were the same, but the links cannot be null. This is not true?

+3
source share
7 answers

& , , . . , - : lvalue .

"" "" ( "pass by pointer" "pass by reference" ) C. ++ .

+7

odp foo var, foo var ().

baz :

foo baz;
opd(baz);

.

+4

, baz. &.

+2

& baz, baz, .

+2

++ , , , , . , , "" "" ++.

int i = 5;
int &j = i;  // j refers to the variable i
// wherever the code uses j, it actually uses i
j++;  // I just changed i from 5 to 6
int *pj = &i;  // pj is a pointer to i
(*pj)--;  // I just changed i back to 5

, pj , j .

int k = 10;
pj = &k;  // pj now actually points to k
(*pj)++;  // I just changed k to 11
j = k;  // no, this doesn't change the reference j to refer to k instead of i,
// but this statement just assigned k to i, that is, i now equals 11!
+1

++ - , . , , .

, , , . , , , . int* int, .

+1

, ? C " ", , - "" . ++ , .

foo baz; 
odp(baz); 
0

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


All Articles