Why nonstatic data initialization is important
Before Modern C++ approach (C++11) we initialized our class something like that.
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> class MyClass { int data1; int data2; public: MyClass(){ this->data1 = 0; this->data2 = 0; } }; |
But Modern C++ brings us this option.
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> class MyClass { int data1 {0}; int data2 {0}; public: MyClass(){ //other codes } }; |
Initializing by using simple curly brackets creates a method which runs before everything includes constructor methods, as a result, we don’t have to do anything for other alternative constructors.
1 Response
[…] Use nonstatic data initialization instead of the old way. (Why nonstatic data initialization is important) […]