中级会员
 
- 积分
- 232
- 金钱
- 232
- 注册时间
- 2017-6-8
- 在线时间
- 35 小时
|
5金钱
实例1代码
#include <iostream>
using namespace std;
#define V(x) var##x
int main()
{
int var1=123,var2=222,var3=321;
printf("%d\n",V(1));
printf("%d\n",V(2));
printf("%d\n",V(3));
return 0;
}
打印
123
222
321
实例2
#include <iostream>
using namespace std;
#define V(x) var##x
int main()
{
int P=1;
int var1=123,var2=222,var3=321;
printf("%d\n",V(P));
return 0;
}
这段代码没法经过编译,报错为:error: 'varP' was not declared in this scope.
究其原因:凡宏定义里有用’#’或’##’的地方宏参数是不会再展开。但我需要它展开成var1 而不是varP.怎么做?有懂的指点一二
|
|