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. VSCode PHP 开发环境配置 详细教程

    VSCode PHP 开发环境配置 详细教程 这篇文章主要介绍了VScode+PHPstudy配置PHP开发环境的步骤,整理了官方以及优秀第三方的内容,对于学习和工作有一定借鉴意义. 配置过程 第一步 ...

  2. js事件常用操作、事件流

    注册事件 给元素添加事件,称为注册事件或者绑定事件. 注册事件有两种方式:传统方式和方法监听注册方式 传统方式 on开头的事件,例如onclick <button onclick="a ...

  3. linux运维思想

    1.安装部署某个服务或者研究某个知识点时,宁可花大量时间,也需要尽量将该服务搞透,后续再遇到相关问题时你会发现这为你节省的时间将远远比你当时花的时间多. 2.安装部署时,做好记录,发本地记录并发表博文 ...

  4. Go defer 原理和源码剖析

    Go 语言中有一个非常有用的保留字 defer,它可以调用一个函数,该函数的执行被推迟到包裹它的函数返回时执行. defer 语句调用的函数,要么是因为包裹它的函数执行了 return 语句,到达了函 ...

  5. dart系列之:元世界pubspec.yaml文件详解

    目录 简介 pubspec.yaml支持的字段 一个例子 字段详情 总结 简介 pubspec.yaml是所有dart项目的灵魂,它包含了所有dart项目的依赖信息和其他元信息,所以pubspec.y ...

  6. [loj2392]烟花棒

    显然,有以下三个性质(思路): 1.烟花传递总是在烟花将要燃尽时将烟花恰传给另一个人 2.烟花不燃烧的人总是向烟花正在燃烧的人靠拢,并且重合后会一直跟着(燃尽时替上) 3.烟花正在燃烧的人总是向下一个 ...

  7. 第02章_MySQL环境搭建

    第02章_MySQL环境搭建 1. MySQL的卸载 步骤1:停止MySQL服务 在卸载之前,先停止MySQL8.0的服务.按键盘上的"Ctrl + Alt + Delete"组合 ...

  8. 微信小程序如何重写Page方法?以及重写Page方法给开发者带来的好处

    17,18年的时候,我当时主要开发小程序,那时候领导想看一下小程序的访问量,还有一些埋点的需求,于是我们的小程序就接入了阿拉丁统计. 阿拉丁的接入方式除了配置以外,主要就一行引入代码.官方要求将以下代 ...

  9. 【SpringBoot】(1)-- 基于eclipse配置springboot开发环境

    基于eclipse配置springboot开发环境 1. 下载并配置eclipse ① 前往eclipse官网 https://www.eclipse.org/downloads/packages/ ...

  10. springbootjpa的dao层也会出现找不到javabean的操作

    使用jpa的过程中,有一次使用dao写了一个 SysCompany findByName(String name);但实体类中没有name这个属性就会报错.bean注入异常