Briefly describe C++11 initialization in place and list initialization

  • 2020-11-18 06:22:42
  • OfStack

1. Local initialization

1.1 introduction

Before C++11, only static constant members of a structure or class could be initialized in place, and nothing else.


class C
{
private:
	static const int a=10;	//yes
	int a=10;					//no
}

In C++11, a data member of a structure or class can be assigned a default value directly when it is declared. There are two ways to initialize 1 with the equal sign "=" and 2 with the brace list. Note that the following code is used as a reference:


class C 
{
private: 
 int a=7; 	//C++11 only
 int b{7}; // or int b={7}; C++11 only
 int c(7);	//error
}; 

Note that the curly brace initialization method cannot be applied to in-place initialization.

1.2 The sequence of in-place initialization and initialization list

The C++11 standard enables the in-place initialization of non-static data members while retaining the initializer list, which means that either in-place or initializer list can be used to complete the initializer of data members. When the two are used together, there is no conflict, and the initialization list occurs after the local initialization, that is, the final initialization result is subject to the initialization list. Refer to the following code:


#include <iostream>
using namespace std;

class Mem
{
public:
	Mem(int i,int j):m1(i),m2(j) {}

	int m1 = 1;
	int m2 = {2};
};

int main()
{
	Mem mem(11,22);
	cout<<"m1="<< mem.m1<<" m2="<<mem.m2<<endl;
}

Program output results:

[

m1=11 m2=22

]

2. List initialization

Before C++11, there are mainly the following initialization methods:


// Braces are initialized 
string str("hello");

// Equal sign initialization 
string str="hello";

//POD Object and the POD Array list initialization 
struct Studnet
{
	char* name;
	int age;
};
Studnet s={"dablelv",18}; // Pure data ( Plain of Data,POD ) Type object 
Studnet sArr[]={{"dablelv",18},{"tommy",19}}; //POD An array of 

// Constructor initializer list 
class Class
{
	int x;
public:
	Class():x(0){} 
};

So many object initializations not only increase learning costs, but also make code styles different, affecting the readability and uniformity of the code. Starting from C++11, the function of list initialization (List Initialization) has been expanded, which can be applied to the initialization of any type of object. Thus, the list initialization method completes the Tiantianda1 system.


class Test
{
 int a;
 int b;
public: 
 C(int i, int j); 
}; 
Test t{0,0};     //C++11 only , which is equivalent to  Test t(0,0); 
Test* pT=new Test{1,2};  //C++11 only , which is equivalent to  Test* pT=new Test{1,2};
int* a = new int[3]{1,2,0}; 	//C++11 only

In addition, C++11 list initialization can also be applied to the container, finally getting rid of the push_back() call, C++11 can visually initialize the container:


//C++11 container initializer
vector<string> vs={"first", "second", "third"};
map<string,string> singers ={{"Lady Gaga", "+1 (212) 555-7890"},{"Beyonce Knowles", "+1 (212) 555-0987"}};

Therefore, the list provided by C++11 can be initialized as the initialization method of unification 1, which not only reduces the difficulty of memory, but also improves the unification 1 degree of code.

The above is a brief introduction to C++11 local initialization and list initialization details, more information on C++11 local initialization and list initialization please pay attention to other related articles on this site!


Related articles: