C++11 introduced serveral contructor-related enhancements including:
- Class member initializers
- Delegating controctors
This article discusses is about Class member initializers Only.
Class member initializers is also called In-class Initializers. C++11 follows other programming language so let's initializer a class member directly during its declaration:
// c++11 int 5; // In-class initializer bool flag (false); // another in-class initializer public : M ();}; M m1; // M1.J = 5, M1.flag = False
The complier transforms every member initializers (such as int j = 5) into a controctor ' s member Initializer. Therefore, the Declaration of class M above is semantically equalment to the following c++03 class Definition:
class m2{ int j; BOOL flag; public : M2 (): J (5), flag (false) {}}
If The constructor includes an explict member initializer for a member that also have an in-class initializer, the CONTROCT or ' s member Intializer would override the In-class Initializer.
class m2{ int7; // In-class initializer public : M2 (); // j = 7 M2 (int i): J (i) {} // overrides J ' s In-class intializer}; M2 m2; // j = 7 M2 m3 (5); // j = 5
Reference:
C++11 tutorial:new Constructor Features that make Object initialization Faster and smoother, smartbear.com
[c + +] In-class initializer