为何没有运行析构函数?编译器为Visual Studio 2019
#include
#include
using namespace std;
class Person
{
public:
// 1、构造函数
// 无返回值,不用写void
// 函数名和类名相同
// 构造函数可以有参数,可以发生重载
// 创建对象的时候,系统会自动调用且只调用一次
Person()
{
cout << "Person构造函数调用" << endl;
}
// 2、析构函数
// 无返回值,不用写void
// 函数名和类名相同,并在类名前加 ~
// 构造函数没有有参数,不可以发生重载
// 销毁对象的时候,系统会自动调用且只调用一次
~Person()
{
cout << "Person析构函数调用" << endl;
}
};
void test01()
{
Person p; // 栈区上的数据,当该函数执行完毕,会释放这个对象
}
int main()
{
test01();
system("pause");
return 0;
}
Person构造函数调用
该代码中,对象Person p位于test01()函数内,该函数执行完毕,会释放这个对象,运行析构函数,但是此处却没有运行
构造函数和析构函数都能运行