C ++与宏替换功能 [英] C++ Replacing Functions with Macros
本文介绍了C ++与宏替换功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们已经在我们的C ++ code现有的功能实现:
We have an existing function implementation in our C++ code:
void Function(int param)
{
printf("In Function\n");
}
int main()
{
Function(10);
return 0;
}
我想改变它调用另一个功能(由宏声明的帮助下),它会接受像额外PARAMS 文件和订单(用于调试目的),然后调用实际功能:
I wish to change it to call another function (by help of a macro declaration) which would accept additional params like FILE and LINE (for debugging purpose) and then call the actual function:
#define Function(param) Function_debug(param, __FILE__, __FUNCTION__, __LINE__) \
{\
printf("In Function_debug [%s] [%s] [%d]\n", file, func, line); \
Function(param);\
}
但低于code:
But the below code:
#include <stdio.h>
#define Function(param) Function_debug(param, __FILE__, __FUNCTION__, __LINE__) \
{\
printf("In Function_debug [%s] [%s] [%d]\n", file, func, line); \
Function(param);\
}
void Function(int param)
{
printf("In Function\n");
}
int main()
{
Function(10);
return 0;
}
语句:
void Function_debug(int param, "temp.cpp", __FUNCTION__, 9) { printf("In Function_debug [%s] [%s] [%d]\n", file, func, line); Function(int param);}
{
printf("In Function\n");
}
int main()
{
Function_debug(10, "temp.cpp", __FUNCTION__, 16) { printf("In Function_debug [%s] [%s] [%d]\n", file, func, line); Function(10);};
return 0;
}
这给编译错误。
请告诉我如何实现这一目标?
Please direct me how to achieve the objective?
推荐答案
通常你会做这样的事情:
Normally you'd do something like this:
#if DEBUG
#define FUNCTION(param) Function_debug(param, __FILE__, __FUNCTION__, __LINE__)
#else
#define FUNCTION(param) Function(param)
#endif
void Function(int param)
{
printf("In Function\n");
}
void Function_debug(int param, const char * file, const char * func, int line)
{
printf("In Function_debug [%s] [%s] [%d]\n", file, func, line); \
Function(param);
}
int main()
{
FUNCTION(10);
return 0;
}
这篇关于C ++与宏替换功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文