C++ default constructor | Built-in types
Predict the output of following program?
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6
7 cout << int() << endl;
8 return 0;
9 }
A constructor without any arguments or with default values for every argument, is treated as default constructor. It will be called by the compiler when in need (precisely code will be generated for default constructor based on need).
C++ allows even built-in type (primitive types) to have default constructors. The function style cast int() is analogous(相似的)to casting 0 to required type. The program prints 0 on console.
The initial content of the article triggered many discussions, given below is consolidation.
It is worth to be cognizant of reference vs. value semantics in C++ and the concept of Plain Old Data types. From Wiki, primitive types and POD types have no user-defined copy assignment operator, no user-defined destructor, and no non-static data members that are not themselves PODs. Moreover, a POD class must be an aggregate, meaning it has no user-declared constructors, no private nor protected non-static data, no base classes and no virtual functions.
An excerpt(摘录) (from a mail note) from the creator of C++, “I think you mix up ‘actual constructor calls’ with conceptually having a constructor. Built-in types are considered to have constructors”.
The code snippet(片段) above mentioned int() is considered to be conceptually having constructor. However, there will not be any code generated to make an explicit constructor call. But when we observe assembly output, code will be generated to initialize the identifier using value semantics. For more details refer section 8.5 of this document.
Thanks to Prasoon Saurav for initiating the discussion, providing various references and correcting lacuna in my understanding.
具体更多内容请查看:http://www.geeksforgeeks.org/c-default-constructor-built-in-types/
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:08:38
C++ default constructor | Built-in types的更多相关文章
- Constructor Overloading in Java with examples 构造方法重载 Default constructor 默认构造器 缺省构造器 创建对象 类实例化
Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Cl ...
- 错误: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 ...
- Default Constructor的构造操作
Default Constructor的构造操作 C++ Annotated Reference Manual书中的Section 12.1说过:default constructor 只有在编译器需 ...
- Eclipse错误:Implicit super constructor ClassName is undefined for default constructor. Must define an explicit constructor
public class Test01 { private String name; private int age; public Test01(String name){ this.name = ...
- C++对象模型(一):The Semantics of Constructors The Default Constructor (默认构造函数什么时候会被创建出来)
本文是 Inside The C++ Object Model, Chapter 2的部分读书笔记. C++ Annotated Reference Manual中明确告诉我们: default co ...
- Unable to find a constructor to use for type System.Security.Claims.Claim. A class should either have a default constructor
Newtonsoft.Json DeserializeObject 反序列化 IdentityServer4.Models Cliecnt 错误: Newtonsoft.Json.JsonSeria ...
随机推荐
- 【GIS】GeoServer服务Authkey配置记录
特别感谢:https://www.cnblogs.com/HandyLi/p/8624507.html 1.服务受控配置 2.授权方式 3.Url模式配置 4.Authkey密钥配置 5.使用 在wm ...
- 全程精髓无废话,腾讯强推Redis深度笔记我粉了
作为目前主流的NoSQL技术,redis在Java互联网中得到了非常广泛的使用,个时代码代码的秃头人员,对Redis肯定是不陌生的,如果连Redis都没用过,还真不好意思出去面试,指不定被面试官吊打多 ...
- Effective C++ 总结笔记(五)
六.继承与面向对象设计 32.确定你的public继承塑模出is-a关系 public继承意味着is-a.适用于base class身上的每一件事情也一定适用于derived class身上.每一个d ...
- 菜鸡的Java笔记 Object 类
Object 类 Object类 的主要作用 Object 类的常用方法 内容 虽然有了对象的向上转型,可以解决了参数的统一问题,但是 ...
- python实现膨胀与腐蚀
目录: (一)膨胀 (二)腐蚀 (三)腐蚀代码(erode) (四)膨胀代码(dilate) (一)膨胀(或) (二)腐蚀(与) (三)腐蚀代码(erode) 1 def erode_demo(ima ...
- PAT A1024题解——高精度大数相加模板
PAT:A1024 Palindromic Number A number that will be the same when it is written forwards or backwards ...
- myeclipse trial expired暂时解决办法
运行以下程序,生成key: import java.io.*; public class MyEclipseGen { private static final String LL = "D ...
- WinForm训练一_改变窗体大小
1 //引用系统命名空间 2 using System; 3 //项目命名空间 4 using System.Collections.Generic; 5 using System.Component ...
- [bzoj5340]假面
修改:维护g[i][j]表示第i个数为j的概率,从前往后转移 转移方程:g[id][i]=g[id][i-1]*p+g[id][i]*(1-p),初始g[i][a[i]]=1 询问:对于每一个人i,输 ...
- ound interface org.elasticsearch.common.bytes.BytesReference, but class was expected
es得版本和本地项目不一致.. 配置es版本,现在使用得是5.2得版本,可是 maven上看到 elasticsearch-rest-high-level-client 最低也得6版本.下载安装高版本 ...