#include<iostream>
class Point
{
private:
int x;
int y;
public:
Point(int i = 0, int j = 0):x(i), y(j) {}
/ * The above initialization list is optional because it can also be written as the following form:
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(11, 22);
std::cout<<"x = "<<t1.getX()<<", ";
std::cout<<"y = "<<t1.getY();
return 0;
}
The above code is a simple presentation for the initial list. X and Y can initialize in the constructor.
However, in some cases, the data member cannot be initialized in the constructor, but only the initialization list can be used. These scenarios are listed below:
1) Non-static constant constant data member initialization:
Const data members must use initialization list. Refer to the member variable T in the following example.
|
Output:
111
2) Reference members' initialization:
Reference members must use the initialization list. Refer to 't' in the example below.
#include<iostream>
using namespace std;
class Test
{
int &t;
public:
TEST (INT & T): T (T) {} // must use initialization list
int getT() { return t; }
};
int main()
{
int x = 22;
Test t1(x);
cout<<t1.getT()<<", ";
x = 33;
cout<<t1.getT()<<endl;
return 0;
}
Output:
22, 33
3) Initializing member objects without the default constructor:
In the following example, the data member 'a' of class 'b' is an object of class 'a', and 'A' does not have the default constructor, then 'b' must use the initial list. To initialize 'a'.
|
Output:
A’s Constructor called: Value of i: 10
B’s Constructor called
If class A has both default construct functions, the parameter constructor is used, when the default constructor is used to initialize 'A', then b does not necessarily need to use the initialization list; If you want to initialize 'a' with the constructor of the parameter, B must use the initial list.
#include <iostream>
class A
{
int i;
public:
A(int);
};
A::A(int arg)
{
i = arg;
std::cout << "A's Constructor called: Value of i: " << i << std::endl;
}
// Class B inherits from Class A
class B : A
{
public:
B(int);
};
B::B(int x) : A(x)
{// must use initialization list
std::cout << "B's Constructor called";
}
int main()
{
B obj(10);
return 0;
}
operation result:#include <iostream>
class A
{
int i;
public:
A(int);
int getI() const
{
return i;
}
};
A :: A (INT I): i (i) // or uses a list of initialization, or uses the THIS pointer
{
}
/ * You can also write the following code
A::A(int i) {
this->i = i;
}
*/
int main()
{
A a(10);
std::cout << a.getI();
return 0;
}
Output:#include <iostream>
class Type
{
public:
Type()
{
std::cout << "constructor called\n";
}
~Type()
{
std::cout << "destructor called\n";
}
Type(const Type & type)
{
std::cout << "copy constructor called\n";
}
Type& operator=(const Type & type)
{
std::cout << "operator= called\n";
return *this;
}
};
// Do not use the initialization list
class MyClass
{
Type variable;
public:
MyClass (Type A) // Assume Type is a class defining a copy constructor and assignment operator
{
variable = a;
}
};
int main()
{
Type type;
MyClass mc(type);
return 0;
}
// use initialization list
class MyClass {
Type variable;
public:
Myclass (Type A): variable (a) {// Assume Type is a class defining a copy constructor and assignment operator
}
};
As can be seen from the above comparison example, using a list of initializations takes a step. That is, if you assign a value in the constructor, you need a COPY CONSTRUCTOR + CONSTRUCTOR + Assignment Operator + Destructor. If you use the initialization list, you only need Copy Constructor + Copy Constructor + Destructor, less A process of calling an assignment operator.
In a real application, if there are many member data, the multi-out copy process will consume a considerable performance.
Summary Use configuration initialization list function, primarily because of two aspects: Higher underlying efficiency. A comparative process, since fewer assignment step, a higher efficiency of the i...
such as: High concurrency competes for shared resources. For example, spikes need to use distributed locks for shared resources such as inventory. If you do not use distributed locks, it is likely to ...
Q: Since the assignment operation referenced in java is itself atomic, why do you need AtomicReference? A: If you only need to change a reference by an assignment, you don't really need an AtomicRefer...
1 Introduction Students who know a little about .NET / C # should know the existence of the IDispose pattern, but they do not know how many students can understand this pattern thoroughly. The landlor...
operation result: in conclusion: 1. What are deep copy and shallow copy, and when do you need deep copy? Deep copy: When the data member of the copied object is a pointer type, the pointer member itse...
About @RequestBody When do you need to use?@RequestBody? The value from the current end is not a complete object, just when the part of the parameters are included in the REQ, no need@RequestBody When...
When do you need to rebuild an index? Indexing can bring boosts to the database in general, but the additional overhead of the index is also not hierarchically, and the reconstruction of the index is ...
When the Chinese first met, they generally liked to ask: "Your surname?" The other party generally replied: "Free of surnames." Is there a noble name for the surname? Indeed, in th...
General constructor can be assigned to a data member, why you need to initialize the list As an example can be evidence to the contrary, some data members and the assignment can not be initi...
See [url=http://www.cnblogs.com/JeffreyZhao] Lao Zhao [/ url] a few days premise to [url=http://www.cnblogs.com/JeffreyZhao /archive/2009/09/02/double-check-failure.html]double-check[/url], mentioned ...