visual studio 2017 vs2017 c++查看析构函数没有执行-解决方法
vs2015
如果点击“本地windows调试器”,就会看不见析构函数执行的结果。
因为程序把所有代码执行完了,才会执行析构函数。如果程序中有 getchar(),或者system("pause")代码,就是让cmd控制台界面暂停,这样程序没有执行完,所以就没有执行到析构函数。
所以VS2015里面执行时,直接按CTRL+F5,或者点击菜单栏的-调试->开始执行(不调试),就可以看到析构函数执行的过程了。
#include <iostream>//txwtech,派生类的构造函数与析构函数2
using namespace std;
class Base
{
public:
Base()
{
b1 = b2=0;
}
Base(int i, int j);
~Base();
void Print()
{
cout << b1 << "," << b2 << ",";//<<endl;
}
private:
int b1, b2;
};
Base::Base(int i, int j)
{
b1 = i;
b2 = j;
cout << "Base的构造函数被调用:" << b1 << "," << b2 << endl;
}
Base::~Base()
{
cout << "析构函数被调用: " << b1 << "," << b2 << endl;
}
class Derived :public Base
{
public:
Derived()
{
d = 0;
}
Derived(int i, int j, int k);
~Derived();
void Print();
private:
int d;
};
Derived::Derived(int i, int j, int k) :Base(i, j), d(k)
{
cout << "derived的构造函数被调用:" << d << endl;
}
Derived::~Derived()//派生类的析构函数
{
cout << "Derived的析构函数被调用咯:" << d << endl;
}
void Derived::Print()
{
Base::Print();
cout << d << endl;
}
int main()
{
Derived objD1(1,2,3),objD2(-4,-5,-6);
objD1.Print();
objD2.Print();
//system("pause");
//getchar();
return 0;
}