我正在使用 visual studio 2013 并打印一些 cout 命令进行测试,当我调试我的代码时,控件进入两个类的析构函数,但它没有在控制台上打印 cout 语句。
#include <iostream>
using namespace std;
class Uni
{
public:
~Uni()
{
cout << "I am in Uni Destructor" << endl;
}
Uni()
{
cout << "I am in Uni Constructor" << endl;
}
};
class Student: public Uni
{
public:
Student()
{
cout << "I am in Student Constructor" << endl;
}
~Student()
{
cout << "I am in Student Destructor" << endl;
}
};
int main()
{
Student s;
system("pause");
return 0;
}
输出:
我在 Uni Constructor
我在Student Constructor中
最佳答案
我想你此时会得到输出,即在 pause
之后。
int main()
{
Student s;
system("pause");
// here; but s is not destroyed yet
return 0;
}
但此时 s
仍未被销毁。当它离开声明它的范围时,即当离开函数 main
时,它将被销毁。
您可以将放入进一步的范围内进行测试。
int main()
{
{
Student s;
} // s will be destroyed here
system("pause");
return 0;
}
关于c++ - 析构函数不在控制台上打印行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47375044/