Classes: In-class member initializers

Syntax

Provide default values

  • Useful for providing default values for its members. This also reduce the number of members that needs to be initialized per constructor.
  • In case the member contains a default initialization and is also set on constructor, the constructor will override the value.

 

Pitfalls

Initialization should be done for all levels in order to work properly.

Below we can see that the class Test contain an in-class initialization, but that class does not have the initialization created further.

 

Quiz – What values will take a and b after the object construction?

Answer:

 

 

You may also like...

Leave a Reply