I. Preface
In C ++ learning, I think everyone has been tortured by variable definitions and statements. For example, I have examined const and variables in several companies I have tested at the university, what are the differences between different types of arrays. I have been used to learning C ++ anyway. Let's take a look at the "glorious history" section ". Let's take a look at a piece of code:
Player pa; // (a) Player pb(); // (b) Player pc = Player(); // (c) Player pd(Player()); // (d) pd = Player() // (e)
A, B, c, and d declare a variable. a can easily understand that it is a variable. B first feels that it is a function declaration. In fact, it is not, but an object declaration. C thought it was calling the operator = value assignment operation? It is not, but an object, and then calls the copy constructor of Player to generate the object pc. D is the same as c. E is the true call of the value assignment operation. Has it been specially designed by these various object declarations. The C ++ 11 initialization list is now available.
II. Introduction
Before reading C ++ 11 initialization, let's first recall the structure initialization in C language. The Code is as follows:
#include
struct Player{ int id; const char* name;};int main() { Player player = {10001, "c++"}; printf("%d, %s\n", player.id, player.name);}
Struct variables can be initialized in the list, which is very convenient. C ++ 11 introduces the initialization list to initialize variables and objects.
Iii. How to Use
1. Built-in System Types
int ia{1}; // (a) int ib = {1}; // (b) int ic(1); // (c) int id = 1; // (d)
Obviously, d is more in line with habits.
std::vector
va{1, 2, 3}; // (a) std::vector
vb = {1, 2, 3}; // (b) std::vector
vc(1, 10); // (c) std::vector
vd{1, 10}; // (d)
The initialization list can make up for the problem that only the same number can be initialized in c. Do not confuse c and d in use.
std::pair
getPlayer() { return {10001, "c++"}; } std::map
players = {{10001, "c++"}, {10002, "java”}};
You can also return the pair type, and initialize the map.
2. Custom type
For initialization of a single parameter, type matching constructor is not required.
Player pa{}; // (a) Player pb; // (b) Player pc(); // (c) Player pd(b); // (d) Player pe = b; // (e) Player pf = {b}; // (f)
For initialization of the above variables, we recommend a. If it is a constructor with parameters, we recommend B. If it is a constructor without parameters.
3. If you want to implement the initialization list constructor, copy function, and assign value function, you must include the header file initializer_list.
class MyClass{public: MyClass(int a):a_(a){ std::cout << "normal initializer list\n"; } MyClass(std::initializer_list
a):b_(a) { std::cout << "initializer list constructor\n"; } MyClass(MyClass& my) { std::cout << "copy constructor\n"; this->a_ = my.a_; this->b_ = my.b_; } MyClass& operator=(MyClass& my) { std::cout << "operator = constructor\n"; this->a_ = my.a_; this->b_ = my.b_; return *this; }private: int a_; std::initializer_list
b_;};
This is a user-defined class with an initialization list constructor class. Below we will learn how to use Class C ++:
MyClass ma{1}; // (a) MyClass mb = {1, 2, 3}; // (b) MyClass mc(2); // (c) MyClass md = b; // (d) MyClass me(c); // (e) MyClass mf{e}; // (f) auto l{2, 2, 3,3}; MyClass mh{l}; // (e) ma = mb; // (h)
The answer is as follows:
Initializer list constructor
Initializer list constructor
Normal constructor list
Copy constructor
Copy constructor
Copy constructor
Initializer list constructor
Operator = constructor
4. Why do I need to initialize the list?
1. Avoid obfuscation of class declarative objects and treat them differently. For C ++, if there is a bracket initialization object, You can google it on your own. It seems that no matter who makes a mistake, be brave enough to admit mistakes or be a good comrade.
2. convenient when initializing multiple variables
3. Avoid data cutting, because implicit conversion is not allowed by initializing the list. For more information, see my
This article.
5. Notes
1. When declaring variables, use less parentheses to make the program more readable.
2. Use the initialization list to prevent implicit conversion and reduce bugs
3. Objects returned through {} are of the const type and cannot be converted.