Function 在退出容器函数之前删除函数的重复调用

Function 在退出容器函数之前删除函数的重复调用,function,duplicates,Function,Duplicates,考虑以下代码段: void MyFun() { SetMe(); // more code if (.. ) { UnSetMe(); return; } // more codes if ( .. ) { UnSetMe(); return ; } // more code UnSetMe(); } 如您所见,该函数首先调用SetMe(),然后在终止之前调用unset me()。现在,在函数必须退出的每个位置添加unset me()有点麻烦,在某些位置可能会错误地忽略它 处理这种情况的改

考虑以下代码段:

void MyFun()
{
SetMe();
// more code
if (.. )
{
 UnSetMe();
return;
}
// more codes
if ( .. )
{
 UnSetMe();
return ;
}
// more code
UnSetMe();
}
如您所见,该函数首先调用SetMe(),然后在终止之前调用unset me()。现在,在函数必须退出的每个位置添加unset me()有点麻烦,在某些位置可能会错误地忽略它

处理这种情况的改进设计是什么。

您可以从MyWrapperFun()调用MyFun(),然后在以下情况下调用UnsetMe():

void MyWrapperFun()
{
    MyFun();
    UnsetMe();
}