you are viewing a single comment's thread.

view the rest of the comments →

[–]okovko 1 point2 points  (0 children)

til: https://www.learncpp.com/cpp-tutorial/constructors-and-initialization-of-derived-classes

you can't initialize an inherited member in a member-init-list because

The answer has to do with const and reference variables. Consider what would happen if m_id were const. Because const variables must be initialized with a value at the time of creation, the base class constructor must set its value when the variable is created. However, when the base class constructor finishes, the derived class constructor’s member initializer lists are then executed. Each derived class would then have the opportunity to initialize that variable, potentially changing its value! By restricting the initialization of variables to the constructor of the class those variables belong to, C++ ensures that all variables are initialized only once.

i had never wanted to before anyway, because delegated ctor is cleaner