Why must a const data member generally be initialized using a constructor initializer list in C++?

Options

  • A. Because a const member cannot be assigned a value after its initialization
  • B. Because const members are automatically static
  • C. Because constructors cannot contain statements
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

A. Because a const member cannot be assigned a value after its initialization

Detailed Explanation

Answer: (A) Because a const member cannot be assigned a value after its initialization

A const data member must receive its value during initialization. It cannot first be created and then assigned a value inside the constructor body.

Therefore, a constructor initializer list is normally used.

Example:

class Test {
const int x;
public:
Test(int n) : x(n) { }
};

Trying to assign x = n; inside the constructor body would not correctly initialize the const member.