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 constructor, compiler doesn’t create the default constructor.
For example, program 1 compiles without any error, but compilation of program 2 fails with error “no matching function for call to `myInteger::myInteger()’ ”
Program 1
1 #include<iostream>
2
3 using namespace std;
4
5 class myInteger
6 {
7 private:
8 int value;
9
10 //...other things in class
11 };
12
13 int main()
14 {
15 myInteger I1;
16 getchar();
17 return 0;
18 }
Program 2
1 #include<iostream>
2
3 using namespace std;
4
5 class myInteger
6 {
7 private:
8 int value;
9 public:
10 myInteger(int v) // parametrized constructor
11 { value = v; }
12
13 //...other things in class
14 };
15
16 int main()
17 {
18 myInteger I1;
19 getchar();
20 return 0;
21 }
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:11:05
Does compiler create default constructor when we write our own?的更多相关文章
- 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 ...
- Constructor Overloading in Java with examples 构造方法重载 Default constructor 默认构造器 缺省构造器 创建对象 类实例化
Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Cl ...
- C++对象模型(一):The Semantics of Constructors The Default Constructor (默认构造函数什么时候会被创建出来)
本文是 Inside The C++ Object Model, Chapter 2的部分读书笔记. C++ Annotated Reference Manual中明确告诉我们: default co ...
- The Semantics of Constructors: The Default Constructor (默认构造函数什么时候会被创建出来)
本文是 Inside The C++ Object Model, Chapter 2的部分读书笔记. C++ Annotated Reference Manual中明确告诉我们: default co ...
- C++ default constructor | Built-in types
Predict the output of following program? 1 #include <iostream> 2 using namespace std; 3 4 int ...
- 错误:Implicit super constructor xx() is undefined for default constructor. Must define an explicit constructor
错误:Implicit super constructor xx() is undefined for default constructor. Must define an explicit con ...
- C++编译器合成Default Constructor的4种情况
笔记C++编译器为编译器需要合成Default Constructor的4种情况. 1,Class A内含Class B对象,Class A没有Default Constructor时会在编译时合成D ...
- Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead
“Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(B ...
- 构造函数语义学之Default Constructor构建操作
一.Default Constructor的构建操作 首先大家要走出两个误区: 1).任何class如果没有定义default constructor,就会被合成一个来. 2).便以其合成出来的def ...
随机推荐
- mybatis之参数传递的方式 | mybatis
1.单个参数(基本类/包装类+String) 这种情况MyBatis可直接使用这个参数,不需要经过任何处理. 一个参数情况下#{}中内容随便写 public Employee getEmployeeB ...
- PTA甲级1094 The Largest Generation (25分)
PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...
- 大一C语言学习笔记(11)---编程篇--写一个程序,可以获取从键盘上输入的的三个数,并能够判断是否可以以这三个数字作为边长来构成一个三角形,如果可以的话,输出此三角形的周长及面积,要求 0 bug;
考核内容: 写一个程序,可以获取从键盘上输入的的三个数,并能够判断是否可以以这三个数字作为边长来构成一个三角形,如果可以的话,输出此三角形的周长及面积: 答案: #include<stdio.h ...
- 环境(6)Linux文件系统二
一:计算机间的数据传输 windows---linux : lrzsz :需要手动安装 yum install lrzsz -y ; rz 将文件从window上传到linux : ...
- Python基础(偏函数)
import functools#functools.partial就是帮助我们创建一个偏函数的,functools.partial的作用就是,把一个函数的某些参数给固定住(也就是设置默认值),返回一 ...
- jdbc pool java连接池技术
1 ConnectPool .java: 2 3 package pool; 4 5 /** 6 * Title: ConnectPool.Java 7 * Description: 连接池治理器 8 ...
- [noi38]游戏
用线段数维护一段区间内的两个信息:1.需要多少经验就可以让有一个人升级,2.等级和.单点修改直接暴力做就可以,区间修改考虑如果这个区间不会产生升级就不递归下去而是打上懒标记. 考虑这个算法的时间复杂度 ...
- [SVN] Branch and Tag
在 SVN 中,如何建立分支以及如何标记Tag. 右键要处理的文件夹,选择 "TortoiseSVN" - "Branch/tag...",进入下面界面: To ...
- Python之99乘法表代码
#coding=utf-8 #左下三角格式输出九九乘法表 for i in range(1,10): for j in range(1,i+1): print " ...
- 三个 AGC D(AGC037D、AGC043D、AGC050D)
大概就 lxr 讲了 4 个 AGC 的 D,有一个以前做过了不算,另外三个都会做罢( 为了避免开三个博客就把它们合并到一起了 AGC 037 D lxr:难度顺序排列大概是 037<043&l ...