Tag: INI test data causes int to add pause ring error
C++11 initialization
Unified initialization Syntax
C++11 The new initialization list std::initializer_list<> type, you can construct the initialization list by using the {} syntax. The initialization list is a constant; once created, its members cannot be changed and the data in the member cannot be altered. The function can use an initialization list as a parameter.
Prior to the introduction of C + + 11, there were different initialization syntaxes. In C + + 11, you can still use these initialization syntaxes, but you can also choose to use the newly introduced unified initialization syntax. The unified initialization syntax is represented by a pair of curly braces {}.
Std::vector<string> v1 = {"Hello", "World", "Welcome"};
Std::vector<int> v2 = {0, 3, 8, 1, 4};
Note: VS2012 does not support unified initialization method {}
In-class member initialization
#define_crt_secure_no_warnings#include<iostream>#include<string>#include<vector>#include<map>classmem{ Public: Mem (intIintj): M (i), N (j)//Initialize the list to M initialization, you can assign the initial value to the const variable, and the reference variable to assign the initial value { //m = i; error, cannot assign value to const variable//n = j; error, cannot assign value to reference variable } intGetm () {std::cout<<"m:"<< m <<Std::endl; } Const intm; int&N;};voidmytest () {intdata =1;//use "=" to initialize non-static ordinary members, or int data{1};Mem mem{2, data};//object member, you can use {} to invoke the constructor when you create the object//Note: vs2012 does not support unified initialization method {}STD::stringName"XYZ");//use () to invoke constructors return;}intMain () {mytest (); System ("Pause"); return 0;}
List initialization
C++11 introduces a new initialization method, called the initialization list Initialize, which is initialized as follows:
#define_crt_secure_no_warnings#include<iostream>#include<string>#include<vector>#include<map>classperson{ Public: std::stringname; intAge ;};voidmytest () {intA[] = {4,5,6}; intb[]{1,3,5};//Note: vs2012 does not support inti = {1}; intj{3};//Note: vs2012 does not support//initialization lists can be used to initialize struct typesPerson P1 = {"Frank", -}; Std::vector<int> Ivec1 (3,4); //other places that are not easy to initialize, such as std<vector> initialization, can only be initialized with constructors if not used in this way , and it is difficult to achieve the effectstd::vector<int> ivec2 = {5,5,5};//Note: vs2012 does not supportstd::vector<int> IVEC3 = {1,2,3,4,5};//Note: vs2012 does not support return;}intMain () {mytest (); System ("Pause"); return 0;}
Prevent type narrowing
Type narrowing refers to an implicit type conversion that causes data content to change or loss of precision. Use list initialization to prevent type narrowing.
#define_crt_secure_no_warnings#include<iostream>#include<string>#include<vector>#include<map>voidmytest () {Const intx =1024x768; Const inty =Ten; CharA = x;//narrowing, but can be compiled by Char*b =New Char(1024x768);//narrowing, but can be compiled by Charc = {x};//err, narrowing, unable to compile Chard = {y};//can be compiled byUnsignedChare{-1};//err, narrowing, unable to compile floatf{7};//can be compiled by intg{2.0f};//err, narrowing, unable to compile float* H =New float{1e48};//err, narrowing, unable to compile floati =1.21;//can be compiled by return;}intMain () {mytest (); System ("Pause"); return 0;}
C++11 initialization