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的更多相关文章

  1. Constructor Overloading in Java with examples 构造方法重载 Default constructor 默认构造器 缺省构造器 创建对象 类实例化

    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Cl ...

  2. 错误: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 ...

  3. C++编译器合成Default Constructor的4种情况

    笔记C++编译器为编译器需要合成Default Constructor的4种情况. 1,Class A内含Class B对象,Class A没有Default Constructor时会在编译时合成D ...

  4. 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 ...

  5. 构造函数语义学之Default Constructor构建操作

    一.Default Constructor的构建操作 首先大家要走出两个误区: 1).任何class如果没有定义default constructor,就会被合成一个来. 2).便以其合成出来的def ...

  6. Default Constructor的构造操作

    Default Constructor的构造操作 C++ Annotated Reference Manual书中的Section 12.1说过:default constructor 只有在编译器需 ...

  7. 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 = ...

  8. C++对象模型(一):The Semantics of Constructors The Default Constructor (默认构造函数什么时候会被创建出来)

    本文是 Inside The C++ Object Model, Chapter 2的部分读书笔记. C++ Annotated Reference Manual中明确告诉我们: default co ...

  9. 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 ...

随机推荐

  1. MySQL 默认隔离级别是RR,为什么阿里这种大厂会改成RC?

    我之前写过一篇文章<为什么MySQL选择REPEATABLE READ作为默认隔离级别?>介绍过MySQL 的默认隔离级别是 Repeatable Reads以及背后的原因. 主要是因为M ...

  2. Jmeter 运行结果的csv文件生成报告

    把运行结果保存到本地,下次可以直接用结果生成测试报告. 一.首先保证脚本能正常运行 二.本地创建csv文件,用来保存运行结果 三.察看结果树,选择本地文件(上一步创建好的csv文件),保存运行结果,如 ...

  3. python实现分水岭算法

    目录: 问题:分水岭算法对图像分割很有作用,怎么把对象分割开来的?分水岭算法是比较完美的分割,跟前面的讲的轮廓不一样! (一)原理 (二)实现 (一)原理 opencv中的分水岭算法是基于距离变换的, ...

  4. [cf1261F]Xor-Set

    构造一棵权值范围恰为$[0,2^{60})$的权值线段树,考虑其中从下往上第$h$层($0\le h\le 60$)中的一个区间,假设其左端点为$l$,即$[l,l+2^{h})$ 这样的一个区间具有 ...

  5. [loj2478]林克卡特树

    原题等价于选择恰好$k+1$条不相交(无公共点)的路径使得边权和最大 证明:对于原题中的最优解,一定包含了k条0边权的边(否则可以将未使用的边删掉,然后将这条路径的末尾与不在同一个连通块内的点连边), ...

  6. AOP声明式事务

    1.spring-dao.xml修改 参考上面工程配置 <?xml version="1.0" encoding="UTF-8"?> <bea ...

  7. List、ArrayList、迭代器、链表、Vector

    1.List接口中的常用方法. List是Collection接口的子接口.所以List接口中有一些特有的方法. void add(int index, Object element) Object ...

  8. vue 3 学习笔记 (七)——vue3 中 computed 新用法

    vue3 中 的 computed 的使用,由于 vue3 兼容 vue2 的选项式API,所以可以直接使用 vue2的写法,这篇文章主要介绍 vue3 中 computed 的新用法,对比 vue2 ...

  9. SpringCloud升级之路2020.0.x版-41. SpringCloudGateway 基本流程讲解(2)

    本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 我们继续分析上一节提到的 WebHandler,经过将请求封装成 ServerWebExc ...

  10. Apache ZooKeeper原理剖析及分布式理论名企高频面试v3.7.0

    概述 **本人博客网站 **IT小神 www.itxiaoshen.com 定义 Apache ZooKeeper官网 https://zookeeper.apache.org/ 最新版本3.7.0 ...