Search code examples
c++initialization-list

Disadvantage to Large Initialization List?


At my employer, it is policy that we use initialization lists in the constructor because it is more efficient.

However, I am developing a class that has 45 data members requiring initialization. Per the policy, this would have to be done in an initialization list in the constructor.

Other than readability, what would be the disadvantage to a large initialization list?


Solution

  • You can format a member initializer list over multiple physical source lines so there doesn't have to be a readability issue.

    The larger issue is obviously the fact that you have classes with 45 data members. Nothing is going to make working with such classes particularly easy.


    AClass::AClass( type1 val1
                  , type2 val2
                  // ...
                  , type45 val45 )
    : mem1( val1 )
    , mem2( val2 )
    // ...
    , mem45( val45 )
    {
    }
    

    I argue is no less readable than:

    AClass::AClass( type1 val1
                  , type2 val2
                  // ...
                  , type45 val45 )
    {
        mem1 = val1;
        mem2 = val2;
         // ...
        mem45 = val45;
    }