3 ms·
&value would be a pointer to that integer. Instead, it's using references, which also use the & character. References use different syntax, but they're like poi
by trealira 2y ago
&value would be a pointer to that integer. Instead, it's using references, which also use the & character. References use different syntax, but they're like pointers that can't be reassigned. Examples:
int x = 0;
int &r = x;
r += 1;
assert(x == 1);
int y;
r = y; // won't compile
void inc(int &r) { r += 1; }
int x = 0;
inc(x);
assert(x == 1);
The equivalent using pointers, like in C:
int x = 0;
int *p = &x;
*p += 1;
assert(x == 1);
int y;
p = &y;
void inc(int *p) { *p += 1; }
int x = 0;
inc(&x);
assert(x == 1);
- trealira 2y agoMistake: r = y; // won't compile This will compile. It will be effectively the same as "x = y". The pointer equivalent is *p = y". My apologies, as it's been a while since I've used C++.