博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++构造与析构(13) - 内建类型的默认构造函数
阅读量:4071 次
发布时间:2019-05-25

本文共 1281 字,大约阅读时间需要 4 分钟。

参考下面的程序输出:

#include 
using namespace std;int main() { cout << "output = " <
<< endl;   return 0;}

输出结果:

output = 0

如果一个构造函数,没有任何参数,或者每个参数都带有默认值,则此函数是默认构造函数。编译器在需要时会调用此函数(还可能在必要时生成一些额外代码)。

C++甚至允许内建类型拥有默认构造函数。上面代码中,函数风格的int()相当于0. 因此程序打印结果为0。

下面是网络上一些关于内建类型的讨论,这里直接引用过来了:

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

转载地址:http://qqeji.baihongyu.com/

你可能感兴趣的文章
IntelliJ IDEA 下的svn配置及使用的非常详细的图文总结
查看>>
【IntelliJ IDEA】idea导入项目只显示项目中的文件,不显示项目结构
查看>>
ssh 如何方便的切换到其他节点??
查看>>
JSP中文乱码总结
查看>>
Java-IO-File类
查看>>
Java-IO-java的IO流
查看>>
Java-IO-输入/输出流体系
查看>>
Java实现DES加密解密
查看>>
HTML基础
查看>>
Java IO
查看>>
Java NIO
查看>>
Java大数据:Hbase分布式存储入门
查看>>
Java大数据:全文搜索引擎Elasticsearch入门
查看>>
大数据学习:Hadoop入门学习书单
查看>>
大数据学习:Spark SQL入门简介
查看>>
大数据学习:Spark RDD操作入门
查看>>
大数据框架:Spark 生态实时流计算
查看>>
大数据入门:Hive和Hbase区别对比
查看>>
大数据入门:ZooKeeper工作原理
查看>>
大数据入门:Zookeeper结构体系
查看>>