← Quiz: C++ Classes and Objects: all 10 questions
Quiz: C++ Classes and Objects · Question 8 of 10
Why should members be set in the constructor's initializer list rather than by assignment in the body?
Why should members be set in the constructor's initializer list rather than by assignment in the body?
- A.The body of a constructor cannot assign values
- B.Members are initialised directly, and const or reference members can only be set there
- C.Initializer lists run after the body, so values are final
- D.It makes the members public
Answer & explanation
Correct answer: Members are initialised directly, and const or reference members can only be set there
Assignment in the body first default-constructs the member, then overwrites it; const and reference members cannot be assigned at all.
Can you get the rest right without peeking?
Take the timed Quiz: C++ Classes and Objects quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →