栈解退最佳示例
所謂的棧解退意思是運(yùn)行結(jié)果非常類似于棧的后進(jìn)先出,代碼示例如下:
#include<iostream>
using namespace std;
class A
{
int x;
public:
A(int x1):x(x1)
{
cout<<"a"<<x<<"被創(chuàng)建"<<endl;
}
~A()
{
cout<<"a"<<x<<"被釋放"<<endl;
}
};
void f1()
{A a1(1);
throw 0;
}
void f2()
{
A a2(2);
f1();
}
void f3()
{
A a3(3);
try
{
f2();
}
catch(int)
{
cout<<"異常被處理"<<endl;
}
A a4(4);
}
int main()
{
f3();
return 0;
}
總結(jié)
- 上一篇: 网易云音乐“十年关注报告”上线,可领取限
- 下一篇: 静态库和动态库(转)