实际开发中经常用到这样的函数
bool test (void)
{
printf("test pass \n");
return true;
}
bool test(int a,int b)
{
printf("test pass 2\n");
return true;
}
这样在使用的时候会这样
bool start_old()
{
bool result =false;
result= test();
if(!result) return false;
result=test(1,2);
if(!result) return false;
return true;
}
如果使用这个宏,
#define checkErr(f) if(!##f) { return false ;}
那么 代码可以简化如下:
bool start()
{
checkErr (test());
checkErr (test(1,2));
return true;
}
代码得到了极大的压缩,并且变得直观,易于理解,完整代码如下,希望可以帮到你。
#include <stdio.h>
#define checkErr(f) if(!##f) { return false ;}
bool test (void)
{
printf("test pass \n");
return true;
}
bool test(int a,int b)
{
printf("test pass 2\n");
return true;
}
bool start_old()
{
bool result =false;
result= test();
if(!result) return false;
result=test(1,2);
if(!result) return false;
return true;
}
bool start()
{
checkErr (test());
checkErr (test(1,2));
return true;
}
int main( int argc, char** argv)
{
start_old();
start();
return 0;
}