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). Compiler created default constructor has empty body, i.e., it doesn’t assign default values to data members (In java, default constructors assign default values).
Compiler also creates a copy constructor if we don’t write our own copy constructor. Unlike default constructor, body of compiler created copy constructor is not empty, it copies all data members of passed object to the object which is being created.
What happens when we write only a copy constructor – does compiler create default constructor?
Compiler doesn’t create a default constructor if we write any constructor even if it is copy constructor.
For example, the following program doesn’t compile.
1 #include <iostream>
2 using namespace std;
3
4 class Point
5 {
6 int x, y;
7 public:
8 Point(const Point &p) { x = p.x; y = p.y; }
9 };
10
11 int main()
12 {
13 Point p1; // COMPILER ERROR
14 Point p2 = p1;
15 return 0;
16 }
Output: COMPILER ERROR: no matching function for call to 'Point::Point()
What about reverse – what happens when we write a normal constructor and don’t write a copy constructor?
Reverse is not true. Compiler creates a copy constructor if we don’t write our own. Compiler creates it even if we have written other constructors in class. For example, the below program works fine.
1 #include <iostream>
2 using namespace std;
3
4 class Point
5 {
6 int x, y;
7 public:
8 Point(int i, int j)
9 {
10 x = 10;
11 y = 20;
12 }
13 int getX()
14 {
15 return x;
16 }
17 int getY()
18 {
19 return y;
20 }
21 };
22
23 int main()
24 {
25 Point p1(10, 20);
26 Point p2 = p1; // This compiles fine
27 cout << "x = " << p2.getX() << " y = " << p2.getY();
28 return 0;
29 }
Output: x = 10 y = 20
So we need to write copy constructor only when we have pointers or run time allocation of resource like file handle, a network connection, etc
下面的内容摘自《深入探索C++对象模型》。
对于class X,如果没有任何user-declared constructor,那么会有一个default constructor被暗中(implicitly)声明出来.....一个被暗中声明出来的default constructor将是一个trivial(浅薄而无能,没哈用的)constructor......
一个nontrivial default constructor就是编译器需要的那种,必要的话会由编译器合成出来。下面分别讨论nontrivial default constructor的四种情况。
(1)“带有Default Constructor”的Member Class Object
如果一个class没有任何constructor,但它内含一个member object,而后者有default constructor,那么这个class的implicit default constructor就是“nontrivial”,编译器需要为此class合成出一个default constructor。不过这个合成操作只有在constructor 真正需要被调用时才会发生。
(2)“带有Default Constructor”的Base Class
类似的道理,如果一个没有任何constructors的class派生自一个“带有default constructor”的base class,那么这个derived class的default constructor会被视为nontrivial,并因此需要合成出来。它将调用上一层base classes的default constructor(根据它们的声明次序)。对一个后继派生的class而言,这个合成的constructor和一个“被明确提供的default constructor”没有什么差异。
(3)“带有一个Virtual Function”的Class
另有两种情况,也需要合成出default constructor:
<1> class声明(或继承)一个virtual function;
<2> class派生自一个继承串链,其中有一个或更多的virtual base class
不管哪一种情况,由于缺乏由user声明的constructors,编译器会详细记录合成一个default constructor的必要信息。
(4)“带有一个Virtual Base Class”的Class
C++新手一般有两个常见的误解:
(1)任何class如果没有定义default constructor,就会被合成出一个来。
(2)编译器合成出来的default constructor会明确设定“class内每一个data member的默认值”。
如你所见,没有一个是真的。
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 19:36:09
When does compiler create default and copy constructors in C++?的更多相关文章
- Does compiler create default constructor when we write our own?
In C++, compiler by default creates default constructor for every class. But, if we define our own c ...
- default constructor,copy constructor,copy assignment
C++ Code 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...
- Selenium遇到问题unknown error:cannot create default profile directory......
1.selenium遇到问题unknown error:cannot create default profile directory...... 2.解决方案 问题1:把驱动放入C:\Windows ...
- C++对象模型(二):The Semantics of Copy Constructors(拷贝构造函数之编译背后的行为)
本文是 Inside The C++ Object Model's Chapter 2 的部分读书笔记. 有三种情况,需要拷贝构造函数: 1)object直接为另外一个object的初始值 2)ob ...
- 面向对象程序设计-C++ Default constructor & Copy constructor& Destructor & Operator Overloading【第九次上课笔记】
先上笔记内容吧: 这次上课的内容有关 构造函数 析构函数 运算符重载 return * this 内容很细,大家好好回顾笔记再照应程序复习吧 :) #include <iostream> ...
- Please configure Spring facet or use 'Create Default Context' to add one including all unmapped files.
有时候我们刚进入 Intellij IDEA时会出现这样一个情况,原因是IDEA没有找到spring的配置文件,我们需要添加spring文件给idea管理 参考: 1.https://www.jetb ...
- SWIG 3 中文手册——6. SWIG 和 C++
目录 6 SWIG 和 C++ 6.1 关于包装 C++ 6.2 方法 6.3 支持的 C++ 功能 6.4 命令行选项与编译 6.5.1 代理类的构造 6.5.2 代理类中的资源管理 6.5.3 语 ...
- Default Constructors
A constructor without any arguments or with default value for every argument, is said to be default ...
- (C++) Interview in English. - Constructors/Destructors
Constructors/Destructors. 我们都知道,在C++中建立一个类,这个类中肯定会包括构造函数.析构函数.复制构造函数和重载赋值操作:即使在你没有明确定义的情况下,编译器也会给你生成 ...
随机推荐
- SimpleNVR流媒体服务在多分屏直播实时阅览时所遇到问题的解决
视频有一个流的概念,称为流媒体.当大量的客户端或WEB访问监控摄像机的时候,大多数的录像机无法承受那么大的网络压力,这时候SimpleNVR流媒体服务器的优势就显示出来了.其能将客户端的访问压力转到服 ...
- 菜鸡的Java笔记 第八 - java 面向对象
面向对象的特点以及开发过程. java中最大的特点是其支持面向对象编程设计思想.在面向对象之前广泛流传的是面向过程的编程思想,例如:C语言的开发就属于面向过程 如果要想更简单的去理解面向过 ...
- N体模拟数据可视化 LightningChart®
N体模拟数据可视化 LightningChart N体模拟也许是目前最先进的数据可视化类型之一.事实上,我们现在谈论的不再是以商业为中心的传统数据的可视化,现在它甚至超越了比如振动分析等先进数据源 ...
- 第十五章---JSON
目录: (一)介绍 (二)Python 编码为 JSON 类型转换对应表 (三)JSON 解码为 Python 类型转换对应表 (四)实例 正文: (一)介绍 JSON (JavaScript Obj ...
- [atARC105D]Let's Play Nim
先对$n$分奇偶两种情况考虑-- $n$为奇数,显然先手希望最终产生的$x_{1}\oplus x_{2}\oplus...\oplus x_{n}=0$ 对于后手,考虑构造:将最大的未被选择的$a_ ...
- Hi3516开发笔记(三):Hi3516虚拟机基础环境搭建之交叉编译环境境搭建以及开机启动脚本分析
前言 前面进行了可以传输,那么写一个简单的C程序来交叉编译并传入运行. 虚拟机 上一篇搭建的虚拟机环境,包含了sftp传递文件,网络能ping通,基于上一篇的虚拟机继续搭建. 海思交叉 ...
- x86汇编反编译到c语言之——(2)if语句
一. 测试的C语句及编译后的x86汇编代码 int a; int b; int main(void) { int c; if (c) a = 4; else b = 5; return 0; } 1 ...
- C# 将Excel转为PDF时自定义表格纸张大小
通过后端程序将Excel表格转为PDF格式时,直接转换后的PDF效果可能出现表格页面过小或者过大,导致页面内容分布不均.要改善转换后的文档效果,只需在转换前自定义表格纸张大小,即可调整转换后的PDF页 ...
- 测试平台系列(85) 把redis运用到实战中
大家好~我是米洛! 我正在从0到1打造一个开源的接口测试平台, 也在编写一套与之对应的完整教程,希望大家多多支持. 欢迎关注我的公众号测试开发坑货,获取最新文章教程! 回顾 上一节我们让支持了前置条件 ...
- 【Azure 环境】在Windows环境中抓取网络包(netsh trace)后,如何转换为Wireshark格式以便进行分析
问题描述 如何在Windows环境中,不安装第三方软件的情况下(使用Windows内置指令),如何抓取网络包呢?并且如何转换为Wireshark 格式呢? 操作步骤 1) 以管理员模式打开CMD,使用 ...