visual studio 2017 vs2017 c++查看析构函数没有执行-解决方法

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;
}

 

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

txwtech笛克特科

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值