References Exercise 5: Find the Compiler Errors
Your turn! Take a look at this snippet…
1 int a = 3;
2 const int b = a;
3 const int& c = a;
4 int& d = a;
5 int& e = b;
6 int& f = c;
7 ++a;
8 ++b;
9 ++c;
10 ++d;
Yikes! That's a lot of ampersands!
Try to stay calm and draw the diagram. It will help you keep it all straight!
These are the lines that would be disallowed by the compiler:
- Line 5 isn't allowed because it tries to make a non-
const
reference (e
) to aconst int
(b
). - Line 6 isn't allowed because it tries to initialize a non-
const
reference (f
) using aconst int
reference (c
, which is a read-only name fora
). - Line 8 isn't allowed because it tries to change the value of a
const int
(b
). - Line 9 isn't allowed because it tries to change the value of a
const int
reference (c
, which is a read-only name fora
).
Just to double-check your diagram, here's a video to walk through it.
If you feel confident that you've got it, feel free to skip the video.
(When logged in, completion status appears here.)