NSDMI: Advantages and Disadvantages

Let's summarize non-static data member initialization.

Advantages of NSDMI

It looks like using NSDMI is a clear winner for Modern C++. Here are the main reasons why is it so helpful:

  • It’s easy to write.
  • You can be sure that each member is initialized correctly.
    • The declaration and the default value are in the same place, so it’s easier to maintain.
    • This also conforms with the rule that every variable should be initialized.
  • It is especially useful when we have several constructors.
    • Previously, we would have to duplicate the initialization code for members or write a custom method, like InitMembers(), that would be called in the constructors.
    • Now, you can do a default initialization, and the constructors will only do their specific jobs.

Get hands-on with 1400+ tech skills courses.