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的更多相关文章

  1. 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 ...

  2. C++对象模型——Default Constructor的建构操作(第二章)

    第2章    构造函数语意学 (The Semantics of Constructor) 关于C++,最常听到的一个抱怨就是,编译器背着程序猿做了太多事情.Conversion运算符就是最常被引用的 ...

  3. 深度探索C++对象模型之第二章:构造函数语意学之Default constructor的构造操作

    C++新手一般由两个常见的误解: 如果任何class没有定义默认构造函数(default constructor),编译器就会合成一个来. 编译器合成的的default constructor会显示的 ...

  4. C++ default constructor | Built-in types

    Predict the output of following program? 1 #include <iostream> 2 using namespace std; 3 4 int ...

  5. TIJ——Chapter Five:Initialization & Cleanup

    Method overloading |_Distinguishing overloaded methods If the methods hava the same name, how can Ja ...

  6. 《深度探索C++对象模型(Inside The C++ Object Model )》学习笔记

    转载:http://dsqiu.iteye.com/blog/1669614 第一章 关于对象 使用class封装之后的布局成本: class并没有增加成本,data members直接内含在每一个c ...

  7. Thinking in Java——笔记(5)

    Initialization & Cleanup Guaranteed initialization with the constructor In Java, the class desig ...

  8. Java性能提示(全)

    http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...

  9. How to Write Doc Comments for the Javadoc Tool

    http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html This document describe ...

随机推荐

  1. JetBrains IntelliJ IDEA汉化

    JetBrains IntelliJ IDEA汉化 开启 IntelliJ IDEA,点击右下角Configure菜单,选择 Plugins.在弹出的 Plugins窗口里,切换至 Marketpla ...

  2. 低代码开发,推荐一款Web 端自动化神器:Automa

    1. Automa介绍 又到了优秀工具推荐的时候了,今天给大家分享一款前端自动化操作神器: Automa . 首先了解一下Automa是什么? Automa它定位是一款 Chrome 插件,也就意味着 ...

  3. 菜鸡的Java笔记 Eclipse 的使用

    Eclipse 的使用    1. Eclipse 简介    2. Eclipse 中的JDT 的使用    3. Eclipse 中的使用 junit 测试        Eclipse (中文翻 ...

  4. R数据分析:如何给结构方程画路径图,tidySEM包详解

    之前一直是用semPlot这个包给来进行结构方程模型的路径绘制,自从用了tidySEM这个包后就发现之前那个包不香了,今天就给大家分享一下tidySEM. 这个包的很大特点就是所有的画图原始都是存在数 ...

  5. Duboo整合SpringBoot超级详细例子(附源码)

    dubbo3.0整合SpringBoot例子 dubbo新版本(3.0以上)在相对于 dubbo 旧版本(2.5.2.6.2.7),有很多的不相同的地方. 官方文档也说了新版本的特性: https:/ ...

  6. [bzoj1032]祖码

    先将连续的一段相同的点连起来,然后考虑对于一段区间,分两种情况:1.首尾两点再同时消掉,必然要先将去掉首尾的一段消掉后再消2.首尾两点再不同时刻消掉,那么必然存在一个断点k,使得k左右无关(题目中的错 ...

  7. opencv 视频处理相关

    包含视频格式知识(编解码和封装格式):如何获取视频信息及视频编解码格式:opencv读取及保存视频,及opencv fourcc编码格式 一.基础知识 视频的编解码格式和封装格式 参考如山似水 视频编 ...

  8. python之元编程

    一.什么是元编程 元编程是一种编写计算机程序的技术,这些程序可以将自己看作数据,因此你可以在运行时对它进行内省.生成和/或修改. Python在语言层面对函数.类等基本类型提供了内省及实时创建和修改的 ...

  9. javaSE基础复习

    第一天:复习java入门知识,jvm内存,java程序执行流程,数据类型,变量和自动类型转换,运算符... 学习java目的:起初是因为想找工作,拿高薪.后来逐渐在学习java技术的过程中渐渐循喜欢上 ...

  10. Atcoder Regular Contst 084 D - XorShift(bitset)

    洛谷题面传送门 & Atcoder 题面传送门 没错,这就是 Small Multiple 那场的 F,显然这种思维题对我来说都是不可做题/cg/cg/cg 首先如果我们把每个二进制数看作一个 ...