我们经常需要在程序的结尾部分上报各种信息, 如果提前在每个if分支进行上报, 无疑会有代码重复:
#include <stdio.h>
int fun()
{
int a = 1;
switch(a)
{
case 1:
// do 1
return 1;
case 2:
// do 2
return 2;
default:
return -1;
}
}
int main()
{
fun();
return 0;
}
那怎么办? 在C++中, 析构函数刚好可以解决这个问题, 最近在析构函数中上报该上报的东西, 用起来很爽。