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 ...
随机推荐
- 登录、注销&用户和用户组管理
登录.注销 # 立刻关机 shutdown -h now # 1分钟后,关机 shutdown -h 1 # 立刻重启 shutdown -r now # 2分钟后,重启 shutdown -r 2 ...
- PTA7-1 迷宫寻路 (20分)
7-1 迷宫寻路 (20分) 给定一个M行N列的迷宫图,其中 "0"表示可通路,"1"表示障碍物,无法通行.在迷宫中只允许在水平或上下四个方向的通路上行走,走过 ...
- 手撸一个IOC容器
IoC 什么是IoC? IoC是Inversion of Control(控制反转)的简称,注意它是一个技术思想.描述的是对象创建.管理的事情. 传统开发方式:比如类A依赖类B,往往会在类A里面new ...
- es聚合查询语法
{ "size": 0, "query": { "bool": { "filter ...
- 剖析虚幻渲染体系(12)- 移动端专题Part 2(GPU架构和机制)
目录 12.4 移动渲染技术要点 12.4.1 Tile-based (Deferred) Rendering 12.4.2 Hierarchical Tiling 12.4.3 Early-Z 12 ...
- 力扣 - 剑指 Offer 12. 矩阵中的路径
题目 剑指 Offer 12. 矩阵中的路径 思路1(回溯.DFS) 这题可以使用回溯+递归来解决,思路如下: 将二维数组的每一个元素都作为起点进行回溯查找 每次查找的时候,都有四个方向,但是上一个方 ...
- CVPR 之 老照片修复
周末闲来无事,随手整理电脑里的照片,望着一张物是人非的老相片,勾起了斑驳的回忆.忽尔转念一想,何不 PS 下,但 PhotoShop 有些大且不免费自己懒得装,于是,转向免费的图像复原软件. 网上搜来 ...
- 【Tool】IntelliJ 搭建Node.js环境
IntelliJ IDEA 开发 Node.js 2019-07-29 14:12:34 by冲冲 1. 配置插件 在IDEA的 file -> setting -> Plugins, ...
- [ Skill ] 图形化组件在注册 User Trigger 时需要注意的事情
https://www.cnblogs.com/yeungchie/ 使用 deRegUserTriggers 可以用来配置:当打开一个新窗口时,自动集成自定义的菜单.工具栏等等. 使用格式如下: d ...
- Codeforces 1500D - Tiles for Bathroom(贪心+队列)
Codeforces 题面传送门 & 洛谷题面传送门 首先先讲一发我的 \(n^2q\log n\) 的做法,虽然没有付诸实现并且我也深知它常数巨大过不去,但是我还是决定讲一讲(大雾 考虑设 ...