What is the difference between Initializing and Assignment inside a constructor?
来源:互联网 发布:linux文件拷贝命令 编辑:程序博客网 时间:2024/06/05 18:51
http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor
Foo(int num): bar(num)
This construct is called a Member Initializer List in C++.
Simply said, it initializes your member bar
to a value num
.
What is the difference between Initializing and Assignment inside a constructor?
Member Initialization:
Foo(int num): bar(num) {};
Member Assignment:
Foo(int num){ bar = num;}
There is a significant difference between Initializing a member using Member initializer list and assigning it an value inside the constructor body.
When you initialize fields via Member initializer list the constructors will be called once and the object will be constructed and initialized in one operation.
If you use assignment then the fields will be first initialized with default constructors and then reassigned (via assignment operator) with actual values.
As you see there is an additional overhead of creation & assignment in the latter, which might be considerable for user defined classes.
Cost of Member Initialization = Object Construction Cost of Member Assignment = Object Construction + Assignment
The latter is actually equivalent to:
Foo(int num) : bar() {bar = num;}
While the former is equivalent to just:
Foo(int num): bar(num){}
For an inbuilt (your code example) or POD class members there is no practical overhead.
When do you HAVE TO use Member Initializer list?
You will have(rather forced) to use a Member Initializer list if:
- Your class has a reference member
- Your class has a non static const member or
- Your class member doesn't have a default constructor or
- For initialization of base class members or
- When constructor’s parameter name is same as data member(this is not really a MUST)
A code example:
class MyClass{ public: //Reference member, has to be Initialized in Member Initializer List int &i; int b; //Non static const member, must be Initialized in Member Initializer List const int k; //Constructor’s parameter name b is same as class data member //Other way is to use this->b to refer to data member MyClass(int a, int b, int c):i(a),b(b),k(c) { //Without Member Initializer //this->b = b; }};class MyClass2:public MyClass{ public: int p; int q; MyClass2(int x,int y,int z,int l,int m):MyClass(x,y,z),p(l),q(m) { }};int main(){ int x = 10; int y = 20; int z = 30; MyClass obj(x,y,z); int l = 40; int m = 50; MyClass2 obj2(x,y,z,l,m); return 0;}
MyClass2
doesn't have a default constructor so it has to be initialized through member initializer list.- Base class
MyClass
does not have a default constructor, So to initialize its member one will need to use Member Initializer List.
Online version of code sample.
Important points to Note while using Member Initializer Lists:
Class Member variables are always initialized in the order in which they are declared in the class.
They are not initialized in the order in which they are specified in the Member Initalizer List.
In short, Member initialization list does not determine the order of initialization.
Given the above it is always a good practice to maintain the same order of members for Member initialization as the order in which they are declared in the class definition. This is because compilers do not warn if the two orders are different but a relatively new user might confuse member Initializer list as the order of initialization and write some code dependent on that.
- What is the difference between Initializing and Assignment inside a constructor?
- What is the difference between Constructor and ngOnInit?
- What is the difference between a thread and a process?
- What is the difference between a definition and a declaration?
- What is the difference between a process and a thread?
- What is the difference between an EXE and a DLL?
- What is the difference between a Generative and Discriminative Algorithm?
- What is the difference between GenericServlet, HttpServlet and a Servlet?
- What is the difference between test, [ and [[ ?
- What is the difference between a = a * (2^3) and a = a << 3?
- What is the difference between a processor, a chip, a socket, and a core?
- What is the difference between a theorem, a lemma, and a corollary?
- What Is the Difference Between a Block, a Proc, and a Lambda in Ruby
- What is the difference between a URI, a URL and a URN
- What is the difference between a von Neumann architecture and a Harvard architecture?
- What is the difference between a pack slip and a pick slip?
- What is the difference between a Bayesian network and a Naive Bayes classifier?
- In SQL, what is the difference between a left join and a left outer join?
- Linux操作系统的安装
- NSMutableString 替换字符串的时候option选择解释
- 回掉函数与函数指针
- 第七周项目三计算圆柱体的表面积
- 配置ospf ipv6
- What is the difference between Initializing and Assignment inside a constructor?
- 关于AjaxSumit提交表单ckeditor的值不实时更新的解决办法
- Java IO流
- XML的4种解析及对比
- Graph cut入门学习
- ios推送详解
- 远程登录协议和ftp有什么区别
- 北京大学公开课:操作系统与虚拟化安全 学习与研究
- 【Axure教程】如何更好的用Axure写产品需求文档