Immutability and Type Conversions

Let's discuss the immutability of constructor parameters and type conversions.

Immutability of constructor parameters

It is not easy to decide whether parameters of reference types should be defined as const or immutable. Although the same considerations apply for constructor parameters as well, immutable is usually a better choice for constructor parameters.

This is because it is common to assign parameters to members to be used at a later time. When a parameter is not immutable, there is no guarantee that the original variable will not change by the time the member gets used.

Let’s consider a constructor that takes a file name as a parameter. The file name will be used for writing student grades. Let’s assume that the constructor parameter is defined as const char[]:

Get hands-on with 1200+ tech skills courses.