C++构造与析构(13) - 内建类型的默认构造函数
2015-05-25 00:48
323 查看
参考下面的程序输出:
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 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.
#include <iostream> using namespace std; int main() { cout << "output = " <<int() << 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 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.
相关文章推荐
- C++构造与析构(4) - 默认构造函数
- C++构造与析构(9) - 默认构造函数
- C++构造与析构(14) - 编译器何时创建默认构造函数和拷贝构造函数
- C++构造与析构(2) - 拷贝构造函数
- C++构造与析构(15) - 为何拷贝构造函数必须为const
- (未完)【C/C++】对象的构造与析构
- C++ 对象构造与析构以及内存布局
- C++ 构造和析构期间不要调用虚函数
- C++ 构造与析构的执行顺序
- C++ - 虚继承(virtual inheritance)的构造(construct)和析构(destruct)顺序
- C++构造、析构、继承、多态--一道题
- C++继承的构造和析构
- C++构造与析构(18) - 静态对象(static object)何时销毁
- C++子类构造和析构被时,父类构造和析构的调用顺序
- 新手笔记本—C++ 继承(public、protected、private)构造与析构
- 深入探索C++对象模型之五 --- 析构、构造、拷贝语意学
- 程序入口函数和glibc及C++全局构造和析构
- c++基础---构造(析构)函数
- [读书笔记] 深入探索C++对象模型-第五章-构造、析构、拷贝语义学(上)
- C++构造与析构(3) - 析构函数