references are nice pointers
TRANSCRIPT
![Page 1: References Are Nice Pointers](https://reader038.vdocument.in/reader038/viewer/2022100807/58ad66fd1a28ab9e428b57db/html5/thumbnails/1.jpg)
References are "Nice" Pointers
void moveBall(ball &b) { b.x += 5; } int main() { ball b; b.x = 10; b.y = 20; moveBall(b); return 0; }
struct ball { int x; int y; };
We previously saw pass-by-reference like this…
![Page 2: References Are Nice Pointers](https://reader038.vdocument.in/reader038/viewer/2022100807/58ad66fd1a28ab9e428b57db/html5/thumbnails/2.jpg)
References are "Nice" Pointers
void moveBall(ball *b) { (*b).x += 5; } int main() { ball b; b.x = 10; b.y = 20; moveBall(&b); return 0; }
…but references are just nice ways of using
pointers.
struct ball { int x; int y; };
![Page 3: References Are Nice Pointers](https://reader038.vdocument.in/reader038/viewer/2022100807/58ad66fd1a28ab9e428b57db/html5/thumbnails/3.jpg)
References are "Nice" Pointers
void moveBall(ball *b) { b->x += 5; } int main() { ball b; b.x = 10; b.y = 20; moveBall(&b); return 0; }
Instead of dereferencing with *, then getting a
member attribute with ., you can use ->
struct ball { int x; int y; };