背景知识
构造函数(Constructor)就是用来构造对象的函数。C++对象在创建的过程中会调用构造函数对类中成员进行初始化。
构造函数包含有普通构造函数、拷贝构造(Copy Constructor)函数以及C++11以后新增的移动构造(Move Constructor)函数三种。通常,我们说构造函数只特指普通构造函数。
示例说明
文中示例均使用下面的自定义Test
类,该类包含int a_
和char *str_
两个成员变量。
class Test {
public: ~Test() {
printf("~Test[%p].[%s]\n", this, str_); delete[] str_; } // ...public: int a_; char *str_;};
测试环境
系统:Windows 10
编译环境:CLion:2020.1.1
编译工具:CMake:CLion 内置
编译器:MinGW:gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
默认情况下gcc编译器会自动优化临时对象构造新对象的行为。为了更好的了解对象的构造流程,需要在CMake中添加如下命令关闭该优化。
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-elide-constructors")
官方解释如下:
The C++ standard allows an implementation to omit creating a temporary that is only used to initialize another object of the same type. Specifying this option[-fno-elide-constructors] disables that optimization, and forces G++ to call the copy constructor in all cases.
对象创建流程
C++可以通过直接声明和new
操作符(operator)两种方式创建对象。这两种方式都会在创建对象的过程中调用构造函数。
直接声明方式
使用形如Test t
方式直接声明创建的对象在栈(stack)中。直接声明创建对象的流程大致分以下两步: