Default Constructors
A constructor without any arguments or with default value for every argument, is said to be default constructor.
What is the significance of default constructor?
Will the code be generated for every default constructor?
Will there be any code inserted by compiler to the user implemented default constructor behind the scenes?
The compiler will implicitly declare default constructor if not provided by programmer, will define it when in need. Compiler defined default constructor is required to do certain initialization of class internals. It will not touch the data members or plain old data types (aggregates like array, structures, etc…). However, the compiler generates code for default constructor based on situation.
Consider a class derived from another class with default constructor, or a class containing another class object with default constructor. The compiler needs to insert code to call the default constructors of base class/embedded object.
1 #include <iostream>
2 using namespace std;
3
4 class Base
5 {
6 public:
7 // compiler "declares" constructor
8 };
9
10 class A
11 {
12 public:
13 // User defined constructor
14 A()
15 {
16 cout << "A Constructor" << endl;
17 }
18
19 // uninitialized
20 int size;
21 };
22
23 class B : public A
24 {
25 // compiler defines default constructor of B, and
26 // inserts stub to call A constructor
27
28 // compiler won't initialize any data of A
29 };
30
31 class C : public A
32 {
33 public:
34 C()
35 {
36 // User defined default constructor of C
37 // Compiler inserts stub to call A's construtor
38 cout << "C Constructor" << endl;
39
40 // compiler won't initialize any data of A
41 }
42 };
43
44 class D
45 {
46 public:
47 D()
48 {
49 // User defined default constructor of D
50 // a - constructor to be called, compiler inserts
51 // stub to call A constructor
52 cout << "D Constructor" << endl;
53
54 // compiler won't initialize any data of 'a'
55 }
56
57 private:
58 A a;
59 };
60
61 int main()
62 {
63 Base base;
64
65 B b;
66 C c;
67 D d;
68
69 return 0;
70 }
Output:
A Constructor
A Constructor
C Constructor
A Constructor
D Constructor
There are different scenarios in which compiler needs to insert code to ensure some necessary initialization as per language requirement. We will have them in upcoming posts. Our objective is to be aware of C++ internals, not to use them incorrectly.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
转载请注明:http://www.cnblogs.com/iloveyouforever/
2013-11-26 10:42:04
Default Constructors的更多相关文章
- When does compiler create default and copy constructors in C++?
In C++, compiler creates a default constructor if we don't define our own constructor (See this). Co ...
- C++对象模型——Default Constructor的建构操作(第二章)
第2章 构造函数语意学 (The Semantics of Constructor) 关于C++,最常听到的一个抱怨就是,编译器背着程序猿做了太多事情.Conversion运算符就是最常被引用的 ...
- 深度探索C++对象模型之第二章:构造函数语意学之Default constructor的构造操作
C++新手一般由两个常见的误解: 如果任何class没有定义默认构造函数(default constructor),编译器就会合成一个来. 编译器合成的的default constructor会显示的 ...
- C++ default constructor | Built-in types
Predict the output of following program? 1 #include <iostream> 2 using namespace std; 3 4 int ...
- TIJ——Chapter Five:Initialization & Cleanup
Method overloading |_Distinguishing overloaded methods If the methods hava the same name, how can Ja ...
- 《深度探索C++对象模型(Inside The C++ Object Model )》学习笔记
转载:http://dsqiu.iteye.com/blog/1669614 第一章 关于对象 使用class封装之后的布局成本: class并没有增加成本,data members直接内含在每一个c ...
- Thinking in Java——笔记(5)
Initialization & Cleanup Guaranteed initialization with the constructor In Java, the class desig ...
- Java性能提示(全)
http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...
- How to Write Doc Comments for the Javadoc Tool
http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html This document describe ...
随机推荐
- Linux 系统分区方案 详细教程
简单分区方案 实际上,很多时候我们只需要分两个区:/和交换分区,日常使用基本不会有任何影响,甚至于交换分区对于现在的电脑来说都不是必要的,我们完全可以只分配一个根分区.linux只需要一个/根分区就可 ...
- uni-app APP端隐藏导航栏自定义按钮
话不多说,上代码 // #ifdef APP-PLUS var webView = this.$mp.page.$getAppWebview(); // 修改buttons webView.setTi ...
- 简单的SQl时间序列生成,每次时间间隔10分钟。
create table #timeseries(Times datetime not null) go declare @firstdate datetime , @lastdate datetim ...
- 通过t-sql定期自动备份SQL Server 上的所有数据库
项目背景 解决方案 方案一,是采用SQL的定时备份,建立作业来操作,这里有完整的使用手册: 方案二:基于t-sql方法进行查询备份 方案思路: 1.1 在 Master 数据库上创建一个备份所有数据库 ...
- CefSharp-基于C#的客户端开发框架技术栈开发全记录
CefSharp简介 源于Google官方 CefSharp用途 CefSharp开发示例 CefSharp应用--弹窗与右键 不弹出子窗体 禁用右键 CefSharp应用--High DPI问题 缩 ...
- Django笔记&教程 4-3 模型(models)主键外键
Django 自学笔记兼学习教程第4章第3节--模型(models)主键外键 点击查看教程总目录 参考:https://docs.djangoproject.com/en/2.2/ref/models ...
- 为什么MySQL字符串不加引号索引失效?《死磕MySQL系列 十一》
群里一个小伙伴在问为什么MySQL字符串不加单引号会导致索引失效,这个问题估计很多人都知道答案.没错,是因为MySQL内部进行了隐式转换. 本期文章就聊聊什么是隐式转换,为什么会发生隐式转换. 系列文 ...
- C/C++ Qt ToolBar 菜单组件应用
ToolBar工具栏在所有窗体应用程序中都广泛被使用,使用ToolBar可以很好的规范菜单功能分类,用户可根据菜单栏来选择不同的功能,Qt中默认自带ToolBar组件,当我们以默认方式创建窗体时,To ...
- Java 操作符小记
"在最底层,Java中的数据是通过使用操作符来操作的" (Thinking in Java) 1 算术操作符 Java 中的基本算术操作符和其他大多数程序设计语言是相同的.其中包括 ...
- Insights直播回顾,对话专家,HMS Core 6创新能力解读
HMS Core Insights第八期直播–对话专家,HMS Core 6创新能力解读,已于11月25日圆满结束,本期直播与小伙伴们一同了解了HMS Core 6在图形.媒体以及连接与通信领域推出的 ...