Learn About How To Use Non-static Data Member Initializers For Windows Apps In C++
Embarcadero Bcc32c and bcc32x (Clang-enhanced compiler for Win32) implements all of the ISO C++11 standard. It includes the use of non-static data member to be initialized where it is declared. The basic idea for C++11 is to allow a non-static data member to be initialized where it is declared (in its class). A constructor can then use the initializer when run-time initialization is needed.
struct B {
B(int, double, double);
};
class A {
int a = 7; // OK
std::string str1 = “member”; // OK
B b = {1, 2, 3.0}; //OK
std::string str2(“member”); // ill-formed
};
#include
struct B { B(int, double, double); };
class A { int a = 7; // OK std::string str1 = “member”; // OK B b = {1, 2, 3.0}; //OK std::string str2(“member”); // ill-formed }; |
Why useful.
-Easier to write.
-You are sure that each member is properly initialized.
-You cannot forget to initialize a member like when having a complicated constructor. Initialization and declaration are in one place – not separated.
-Especially useful when we have several constructors.
-Previously we would have to duplicate initialization code for members.
-Now, you can do a default initialization and constructors will only do its specific jobs.
If a member is initialized by both an in-class initializer and a constructor, only the constructor’s initialization is done (it “overrides” the default).