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

最新文章

  1. 基于redis分布式锁实现“秒杀”
  2. Portlet简述
  3. Leetcode: Number of Boomerangs
  4. [转]使用 C 编写 Lua 模块
  5. centos6.5+jexus5.6.3+mono 3.10实践,让asp.net在linux上飞一会儿
  6. easyui 点击combox 文本框 显示下拉 panel
  7. uva1220--树的最大独立集+判重
  8. 设计模式,Let&#39;s “Go”! (中)
  9. Maven 本地仓库明明有jar包,pom文件还是报错解决办法
  10. Java开发笔记(八十六)通过缓冲区读写文件
  11. 【Android入门】一个App学会安卓开发
  12. python全栈开发 * 15知识点汇总 * 180621
  13. Spring Bean 的生命周期,如何被管理的?
  14. HashMap中hashCode()和equals()重要性
  15. mac 常用开发软件列表
  16. 解决部分小程序无法获取UnionId的问题
  17. js Json与对象、数组转化
  18. 2018年美国大学生数学建模竞赛(MCM/ICM) A题解题思路
  19. window常用的快捷键
  20. JMeter测试WEB性能入门

热门文章

  1. grpc协议
  2. 深入剖析 RocketMQ 源码 - 消息存储模块
  3. 17.彻底解决Jmap在mac版本无法使用的问题
  4. spring security 之自定义表单登录源码跟踪
  5. [hdu6984]Tree Planting
  6. [nowcoder5668H]Sort the Strings Revision
  7. 语音合成论文翻译:2019_MelGAN: Generative Adversarial Networks for Conditional Waveform Synthesis
  8. nginx得请求转发代码-将请求转发到网关
  9. 31、下一个排列 | 算法(leetode,附思维导图 + 全部解法)300题
  10. HTML四种定位-固定定位