In this specific case, is there a difference between using a member initializer list and assigning values in a constructor?(在这种特定情况下,使用成员初始值设定项列表和在构造函数中赋值有区别吗?)
问题描述
在内部和关于生成的代码,有没有真正的区别:
Internally and about the generated code, is there a really difference between :
MyClass::MyClass(): _capacity(15), _data(NULL), _len(0)
{
}
和
MyClass::MyClass()
{
_capacity=15;
_data=NULL;
_len=0
}
谢谢...
推荐答案
假设这些值是原始类型,那么不,没有区别.初始化列表仅在您将对象作为成员时才会产生影响,因为初始化列表允许您将对象初始化为其最终值,而不是使用默认初始化后赋值.这实际上可以明显更快.
Assuming that those values are primitive types, then no, there's no difference. Initialization lists only make a difference when you have objects as members, since instead of using default initialization followed by assignment, the initialization list lets you initialize the object to its final value. This can actually be noticeably faster.
这篇关于在这种特定情况下,使用成员初始值设定项列表和在构造函数中赋值有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在这种特定情况下,使用成员初始值设定项列表和在构造函数中赋值有区别吗?


- 如何对自定义类的向量使用std::find()? 2022-11-07
- STL 中有 dereference_iterator 吗? 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 静态初始化顺序失败 2022-01-01
- 近似搜索的工作原理 2021-01-01
- C++ 协变模板 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 从python回调到c++的选项 2022-11-16