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

最新文章

  1. Linux下搭建coreseek(sphinx+mmseg3)全文检索
  2. python之路六
  3. SSH框架使用中存在的诡异异常
  4. Apache2 部署 Django
  5. [Java] 内部类的用法
  6. 安装builderRobot到Rational Functional Tester和Performance Tester
  7. MINA实战
  8. poj 1080 dp
  9. phantomjs初入门
  10. 未知宽高图片水平垂直居中在div
  11. 【Oracle学习笔记】定时任务(dbms_job)
  12. C#中的PropertyGrid绑定对象,通过改变某一值而动态设置部分属性的特性
  13. Python——IPython和NumPy
  14. C++(实验二)
  15. Android UiAutomator2.0
  16. go操作redis
  17. 2018年你需要知道的13个JavaScript工具库
  18. 写jQuery插件该注意的
  19. maven 新建项目时报错“Could not calculate build plan: Failure to transfer org.apache.maven.plugins:。。。。。。。。。。。。。。”
  20. template-web.js 引用变量、函数

热门文章

  1. 04 | 函数扩展 | es6
  2. LeetCode 114. 二叉树展开为链表 C++
  3. Windows内核中的CPU架构-7-陷阱门(32-Bit Trap Gate)
  4. 深入理解Spring IOC容器
  5. 4.ASCII码排序
  6. 逐浪CMSv8.2发布-集成Node与Vue脚手架和PowerShell支持的新一代网站管理系统
  7. scrapy获取58同城数据
  8. java meil
  9. php 图像和水印
  10. [gym102412D]The Jump from Height of Self-importance to Height of IQ Level