GeekForGeeks 何时在c++中使用初始化列表
2015-01-09 02:28
543 查看
When do we use Initializer List in C++?
Initializer List is used to initialize data members of a class. The list of members to be initialized is indicated with constructor as a comma separated list followed by a colon. Following is an example that uses initializerlist to initialize x and y of Point class.
简单的初始化列表的语法和例子如下:
List must be used. Following are such cases:
下面我们就来介绍一下在什么情况下必须使用初始化列表
1) For initialization of non-static const data members:(初始化类中的非静态的常量数据成员)
const data members must be initialized using Initializer List. In the following example, “t” is a const data member of Test class and is initialized using Initializer List.
类中的常量数据成员能且只能使用初始化列表进行初始化。
Reference members must be initialized using Initializer List. In the following example, “t” is a reference member of Test class and is initialized using Initializer List.
类中的引用成员能且只能使用初始化列表进行初始化。
In the following example, an object “a” of class “A” is data member of class “B”, and “A” doesn’t have default constructor. Initializer List must be used to initialize “a”.
如果你想要带参数地初始化B中的成员变量a,必须使用初始化列表。
4) For initialization of base class members : Like point 3, parameterized constructor of base class can only be called using Initializer List.
参数化初始基类对象
If constructor’s parameter name is same as data member name then the data member must be initialized either using this pointer or
Initializer List. In the following example, both member name and parameter name for A() is “i”.
It is better to initialize all class variables in Initializer List instead of assigning values inside body. Consider the following example:
1. Type’s constructor is called first for “a”.(参数a的Type构造函数调用)
2. The assignment operator of “Type” is called inside body of MyClass() constructor to assign(赋值构造函数调用)
variable = a;
3. And then finally destructor of “Type” is called for “a” since it goes out of scope.(参数a的Type析构函数调用)
Now consider the same code with MyClass() constructor with Initializer List
1. Copy constructor of “Type” class is called to initialize : variable(a). The arguments in initializer list are used to copy construct “variable” directly.
拷贝构造函数调用
2. Destructor of “Type” is called for “a” since it goes out of scope.
析构函数调用
As we can see from this example if we use assignment inside constructor body there are three function calls: constructor + destructor + one addition assignment operator call. And if we use Initializer List there are only two function calls: copy constructor
+ destructor call. See this post for a running example on this point.
This assignment penalty will be much more in “real” applications where there will be many such variables. Thanks to ptr for adding this point.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
相关文章推荐
- C++何时要使用成员初始化列表
- C++中使用初始化列表比在构造函数中对成员变量赋值更高效
- C++中成员初始化列表的使用
- C++中成员初始化列表的使用
- 关于使用C++的成员初始化列表
- C++中使用初始化列表比在构造函数中对成员变量赋值更高效
- C++中成员初始化列表的使用
- 在C++拷贝构造函数中使用初始化列表
- (转) C++中成员初始化列表的使用
- C++中成员初始化列表的使用
- C++:四种必须使用初始化列表情况
- <转载自刘佳ID:freedom0203和waret> C++中成员初始化列表的使用
- 何时构造函数必须使用初始化列表而不是赋值
- [原创]C++中成员初始化列表的使用!!!
- C++中的构造函数初始化列表的使用和分析
- 何时使用构造函数初始化列表?
- &lt;转载自刘佳ID:freedom0203和waret&gt; C++中成员初始化列表的使用
- C++中成员初始化列表的使用
- C++中成员初始化列表的使用
- C++ 必须使用初始化列表