The list of initializers is used to initialize the data members of a class. The constructor represents the list of members to be initialized as a comma-separated list followed by a colon. Here is an example of initializing x and y of the Point class using the list of initializers:
#include<iostream>using namespace std; class Point {private:int x; int y; public:Point(int i = 0, int j = 0):x(i), y(j) {}/* The above use of Initializer list is optional as theconstructor can also be written as:Point(int i = 0, int j = 0) {x = i; y = j; }*/int getX() const {return x; }int getY() const {return y; }}; int main() {Point t1(10, 15); cout<<"x = "<<t1.getX()<<", "; cout<<"y = "<<t1.getY(); return 0; }/* OUTPUT:x = 10, y = 15*/Copy the code
The code above is just an example of the syntax for initializing a list. In the above code, x and y can also be easily initialized in the constructor. However, in some cases, initialization of data members inside the constructor does not work properly and Initializer List must be used. Here’s what happens:
1) For non-static const data members
Initialize ** : ** Must use Initializer List to initialize const data members. In the following example, “t” is a const data member of the Test class and is initialized using an initializer list. The reason we initialize a const data member in the initializer is because no memory is allocated for the const data member separately, so it is collapsed into the symbol table, so we need to initialize it in the initializer.
Again, it’s a parameterized constructor, and we don’t need to call the assignment operator, which means we avoid an extra operation.
#include<iostream>using namespace std; class Test {const int t; public:Test(int t):t(t) {} //Initializer list must be usedint getT() { return t; }}; int main() {Test t1(10); cout<<t1.getT(); return 0; }/* OUTPUT:10*/Copy the code
2) Initialization of reference members:
The initializer list must be used to initialize reference members. In the following example, “t” is a reference member of the Test class and is initialized using the initializer list.
// Initialization of reference data members#include<iostream>using namespace std; class Test {int &t; public:Test(int &t):t(t) {} //Initializer list must be usedint getT() { return t; }}; int main() {int x = 20; Test t1(x); cout<<t1.getT()<<endl; x = 30; cout<<t1.getT()<<endl; return 0; }/* OUTPUT:2030*/Copy the code
3) Initialization of member objects without a default constructor:
In the following example, object “A” of class “A” is A data member of class “B”, and “A” has no default constructor. The initializer list must be used to initialize “A”.
#include <iostream>using namespace std; class A {int i; public:A(int ); }; A::A(int arg) {i = arg; cout << "A's Constructor called: Value of i: " << i << endl; }// Class B contains object of Aclass B {A a; public:B(int ); }; B::B(int x):a(x) { //Initializer list must be usedcout << "B's Constructor called"; }int main() {B obj(10); return 0; }/* OUTPUT:A's Constructor called: Value of i: 10B's Constructor called*/Copy the code
If class A has both A default constructor and A parameterized constructor, then if you want to initialize “A” with the default constructor, you do not have to use the “initializer list”; instead, you must initialize “A” with the parameterized constructor.
** As in point 3, only the Initializer List can be used to call the parameterized constructor of the base class.
#include <iostream>using namespace std; class A {int i; public:A(int ); }; A::A(int arg) {i = arg; cout << "A's Constructor called: Value of i: " << i << endl; }// Class B is derived from Aclass B: A {public:B(int ); }; B::B(int x):A(x) { //Initializer list must be usedcout << "B's Constructor called"; }int main() {B obj(10); return 0; }Copy the code
5) When the constructor parameter name is the same as the data member
If the constructor parameter name is the same as the data member name, the data member must be initialized using this pointer or an initializer list. In the following example, both the member name and parameter name of A () are “I”.
#include <iostream>using namespace std; class A {int i; public:A(int ); int getI() const { return i; }}; A::A(int i):i(i) { } // Either Initializer list or this pointer must be used/* The above constructor can also be written asA::A(int i) {this->i = i; }*/int main() {A a(10); cout<<a.getI(); return 0; }/* OUTPUT:10*/Copy the code
6) For performance reasons:
It is better to initialize all class variables in the Initializer List rather than assign values within the body. Consider the following example:
// Without Initializer Listclass MyClass {Type variable; public:MyClass(Type a) { // Assume that Type is an already// declared class and it has appropriate// constructors and operatorsvariable = a; }};Copy the code
Here, the compiler creates type MyClass by following these steps
Object of 1. The constructor of a type is first called “a”.
2. Inside the MyClass () constructor, call the assignment operator of the “type” to assign
Variable = a;
3. Then, because the destructor of “type” is out of scope, it ends up being called “A”.
Now consider using the same code as the MyClass () constructor with a list of initializers
// With Initializer Listclass MyClass {Type variable; public:MyClass(Type a):variable(a) { // Assume that Type is an already// declared class and it has appropriate// constructors and operators}};Copy the code
Using the Initializer List, the compiler performs the following steps:
1. Call the parameterized constructor of class “Type” to initialize: variable (a). The parameters in the initializer list are used to construct “variables” directly by copying.
2. The destructor for “type” is called “A” because it is out of scope.
As you can see from this example, if assignment is used within the constructor body, there are three function calls: constructor + destructor + an additional assignment operator call. If we use the Initializer List, there are only two function calls: the copy constructor + the destructor call.
And that’s all for today. I hope I can help you
In addition, if you want to improve your programming ability, learn C language C++ programming! Overtake on the curve, one step faster! The author here may be able to help you ~
C language C++ programming learning exchange circle, **QQ group [951258402] ** wechat public number: C language programming learning base
Share (source code, project actual combat video, project notes, basic introduction tutorial)
Welcome to change careers and learn programming partners, use more information to learn and grow faster than their own thinking oh!
Programming learning video sharing: