There were two initialization before c++11:
- Using parentheses
- Using assignment operator
Now c++11, introduced new initialization that is “Uniform initialization“, which mean one common way of intialization for all data types even user defined and its syntax is braces{}.
Using “Uniform initialization” we can initialize all data type, please check below code:
Note: We can use “Uniform initialization” for user defined Object using std::initializer_list but we need define constructor to handle this request, it will be handled by initializer-list constructor.
//Interger int i_val01{1}; std::cout<<"i_val01:"<<i_val01<<std::endl; //Float float f_val01{1.1}; std::cout<<"f_val01:"<<f_val01<<std::endl; //String std::string s_val01{"test"}; std::cout<<"s_val01:"<<s_val01<<std::endl; //Vector std::vector<int> v_vec{1, 2, 3}; std::cout<<"v_vec[0]:"<<v_vec[0]<<std::endl; //User defined Object, we can initialize with {} but //we need define constructor to handle this request //This is NOT good option for object creation //I will explain in other topic test obj{1}; std::cout<<"class member value:"<<obj.getVal()<<std::endl; //Initialization with empty int i_val02{}; std::cout<<"i_val02:"<<i_val02<<std::endl; //Pointer initialized with nullptr int* ip_Val{}; std::cout<<"ip_val:"<<ip_Val<<std::endl;
Advantages:
- Empty braces initialize the variable with proper type so we can get ride from uninitialized variable problem,please check above code.
- Stop implicit or automatic conversion like float is converted to int automatically, please check below error.
//int i_val04{1.1}; //std::cout<<"i_val04:"<<i_val04<<std::endl; //error: narrowing conversion of ‘1.1000000000000001e+0’ from ‘double’ to ‘int’
Complete source code with example:
#include<iostream> #include<vector> class test { int m_i; public: test(const std::initializer_list<int>& list) { for(const auto& ele : list) { m_i = ele; } } int getVal(){return m_i;} }; int main() { std::cout<<"Uniform Initialization"<<std::endl; //Interger int i_val01{1}; std::cout<<"i_val01:"<<i_val01<<std::endl; //Float float f_val01{1.1}; std::cout<<"f_val01:"<<f_val01<<std::endl; //String std::string s_val01{"test"}; std::cout<<"s_val01:"<<s_val01<<std::endl; //Vector std::vector<int> v_vec{1, 2, 3}; std::cout<<"v_vec[0]:"<<v_vec[0]<<std::endl; //User defined Object, we can initialize with {} but //we need define constructor to handle this request test obj{1}; std::cout<<"class member value:"<<obj.getVal()<<std::endl; //Initialization with empty int i_val02{}; std::cout<<"i_val02:"<<i_val02<<std::endl; //Pointer initialized with nullptr int* ip_Val{}; std::cout<<"ip_val:"<<ip_Val<<std::endl; //int i_val03{2, 2 }; //std::cout<<"i_val03:"<<i_val03<<std::endl; //error: scalar object ‘i_val03’ requires one element in initializer //int i_val04{1.1}; //std::cout<<"i_val04:"<<i_val04<<std::endl; //error: narrowing conversion of ‘1.1000000000000001e+0’ from ‘double’ to ‘int’ return 0; }
Output:
Uniform Initialization i_val01:1 f_val01:1.1 s_val01:test v_vec[0]:1 class member value:1 i_val02:0 ip_val:0