用宏定义还是函数?
一个类有30个成员函数,每个成员函数开头都是这段代码:
if(count == 0)
{
int ret = init();
if(ret == fail)
{
return 1;
}
}
if(count == 1)
{
int ret = create();
if(ret == fail)
{
return 1;
}
}
为了简化代码,有两个方法
1、用宏,将上面的代码封起来。
2、写一个函数
int check()
{
if(count == 0)
{
int ret = init();
if(ret == fail)
{
return 1;
}
}
if(count == 1)
{
int ret = create();
if(ret == fail)
{
return 1;
}
}
return 0;
}
用宏可以简化,但是感觉很别扭,因为宏里有两个函数。一般宏只是定义个常值或者固定的运算,一般不带函数较好吧。
用函数的话,感觉效率会低一些
大家说说该怎么办比较好?