ConstReferenceImpliesImmutability

Misconception:

If a const variable refers to an object, that object is immutable.

Incorrect

An object referred to by a const variable is an immutable object

Correct

An object referred to by a const variable can be a mutable object

Correction
Here is what's right.

Having a constant reference to an object or an array does not mean that that object or array is immutabile. The state of the object or the array can change: it is the variable that cannot.

Consider this example in which we declare a constant reference to an array and then change its second element. This is perfectly fine.

Using const prevents changes to the variable itself, like we are attempting to do in the next example in which we reassign a to a different array.

Symptoms
How do you know your students might have this misconception?

Watch a real student who might have this misconception solving a programming exercise!

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.