c++ - C++ 中的析构函数输出
问题描述
以下代码不会打印“调用了析构函数”。为什么?但是,我的书把它打印出来了。如何打印析构函数语句?请建议。
#include <iostream>
using namespace std;
class HelloWorld
{
public:
//Constructor
HelloWorld()
{
cout<<"Constructor is called"<<endl;
}
//Destructor
~HelloWorld()
{
cout<<"Destructor is called"<<endl;
}
};
int main()
{
//Object created
HelloWorld obj;
system("PAUSE");
return 0;
}
解决方案
尝试这个:
int main()
{
{ // start a scope
//Object created
HelloWorld obj;
} // scope ends here, obj will be deleted here
// now pause so you can see the output before program ends.
system("PAUSE");
return 0;
}
推荐阅读
- tensorflow - TensorFlow Blas GEMM 启动失败
- json - 如何在 scalaj 中发布 json 数据?
- android - 如何从 JSON 数组中获取 JSON 数据?
- css - 更改背景颜色
- sql-server - 比较多个可为空日期列的 SQL 日期范围
- javascript - 突出显示当前日期
- python-3.x - 在列表中标记元素
- java - 难以理解 Java 正则表达式解释
- docker - 使用符号链接将日志文件指向 Kubernetes 上的 /proc/1/fd/1 时,STDOUT 日志不起作用
- python - `asyncio.new_event_loop` 的用例是什么?