Initialization form of initial value in C + + class

Posted by penguinboy on Sat, 22 Jan 2022 07:55:09 +0100

C++11 specifies that an in class initial value can be provided for data members. When an object is created, the initial value in the class is used to initialize the data member. As shown below, the initial values in the class of cursor and height are both 0.

class Screen
{
private:
	int cursor = 0;
	int height = 0;
};

I The reason why in class initializers cannot be given in parentheses

C + + primer (version 5) wrote that the initial value in a class must be provided in the form of = or curly brackets {}. Parentheses () cannot be used.

In this regard, you can refer to an online statement as follows:
Since the following situation cannot be avoided, this is equivalent to int z(int); Function, so C + + defines the in class initial value with parentheses as illegal.

class Widget 
{
private: 
  typedef int x;  
  int z(x);
};

II= And curly bracket {} initialization

1. Built in type and class type

For the two, there is little difference between = and {} initialization. The test is as follows.

#include <iostream>     // std::cout
#include <string>
#include <vector>

using namespace std;

int main() 
{
	std::cout << "*****Built in type*****" << std::endl;

	int ival = 1;

	int ival2{ 1 };
	int ival3 = { 1 };//It's actually the same as ival2 {1}

	std::cout <<"ival = " <<ival << std::endl;
	std::cout <<"ival2 = " <<ival2 << std::endl;
	std::cout <<"ival3 = " << ival3 << std::endl;

	std::cout << "*****class*****" << std::endl;

	string str = "aa";

	string str2{ "aa" };
	string str3 = { "aa" };//It's actually the same as STR2 {"AA"}

	std::cout << "str = " << str << std::endl;
	std::cout << "str2 = " << str2 << std::endl;
	std::cout << "str3 = " << str3 << std::endl;

	system("pause");
	return 0;
}

2. Class template

For class templates, we can look at the process of actually initializing the list {} and = initialization:

First look at the initialization list {}:

std::vector<std::string> vs{ "ssss" ,"dddd"};

As you can see, the last step of the above initialization is to insert the elements in the initialization list in the vector

Let's look at the initialization process of =:

Because it is actually a copy construction process, the right side of = must be a value of the same type, as shown below. vs3 can be regarded as a variable of type 'std::vector', so it can only be initialized with a variable of type 'std::vector', such as the following vs_tmp.

	std::vector<string> vs_tmp;
	std::vector<string> vs3 = vs_tmp;

Note: never do this: STD:: vector < string > VS2 = "EEE";
The above initialization is called the copy constructor of vector, as follows:

Summary:

  • Initialize the list. The elements in the list are the elements of the template, such as vector < string > TMP_ vec= {"ddd","www"}
  • =For initialization, only the same type can be used. For example, only vector type can be used to initialize vector type

III Initialization list {} method of initial values in class

I found the following problem when debugging: the compiler will report an error in the initialization list {} mode of the initial value in the class. The classes defined are as follows.
This makes me confused, because it is OK in the above test? Later, I found that because I use VS2013, which does not fully support the C++11 standard, I may not support the initialization list for the initial values in the class. After changing a compiler, no error will be reported.

class Wind_mgr
{
public:
	void putsData()
	{
		str.push_back("aaa");
	}
private:
	std::vector<std::string> str{"bbb"};//Error, unable to convert from "const char [4]" to "const STD:: allocator < _ty >"
};

Finally, several initialization forms of vector are attached:

Initialization formatexplain
vector v1Generate an empty vector
vector v2(V1)v2 contains copies of all elements of v1
vector v2 = v1Equivalent to V2(V1)
vector v3(n,val)v3 is initialized to n val values, that is, v3 has n duplicate elements
vector v4(n)v4 is a vector with n elements, but the n elements are initialization values
vector v5{a,b,c...}The size of v5 is the size of the initialization list, and the value of each element is the value of the initialization list
vector v5 = {a,b,c...}Equivalent to V5{a,b,c...}
vector v6 (first, last)Copy the elements of the iterator [first,last) interval to v6

Topics: C++