CS 70

References Exercise 5: Find the Compiler Errors

  • LHS Cow speaking

    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;
  • Hedgehog speaking

    Yikes! That's a lot of ampersands!

  • LHS Cow speaking

    Try to stay calm and draw the diagram. It will help you keep it all straight!

Which lines would be disallowed by the compiler?

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 a const int (b).
  • Line 6 isn't allowed because it tries to initialize a non-const reference (f) using a const int reference (c, which is a read-only name for a).
  • 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 for a).
  • LHS Cow speaking

    Just to double-check your diagram, here's a video to walk through it.

  • RHS Cow speaking

    If you feel confident that you've got it, feel free to skip the video.

(When logged in, completion status appears here.)