我试图为下面的代码编写宏,但是测试字用红色下划线标记,我不知道声明的宏是否正确。 代码是:
ABC Test{
int x,y,z;
AAA:
Test() : INIT(x),INIT(y),INIT(z) { }
CREATE(x);
CREATE(y);
CREATE(z);
};
我这个ABC词应该等同于“ class”,而AAA是“ public”。 我写的宏是:
#define ABC class name\
int _DATE_
#define AAA public
#define INIT(number)\
{int number = _COUNTER_}
#define ZERO 0
#define CREATE(number) number = ZERO;
答案 0 :(得分:1)
如果m.cc是
#define ABC class name\
int _DATE_
#define AAA public
#define INIT(number)\
{int number = _COUNTER_}
#define ZERO 0
#define CREATE(number) number = ZERO;
ABC Test{
int x,y,z;
AAA:
Test() : INIT(x),INIT(y),INIT(z) { }
CREATE(x);
CREATE(y);
CREATE(z);
};
然后(当然也可以使用 gcc ):
pi@raspberrypi:/tmp $ g++ -E m.cc
# 1 "m.cc"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "m.cc"
# 9 "m.cc"
class nameint _DATE_ Test{
int x,y,z;
public:
Test() : {int x = _COUNTER_},{int y = _COUNTER_},{int z = _COUNTER_} { }
x = 0;;
y = 0;;
z = 0;;
};
如此缩进:
class nameint _DATE_ Test{
int x,y,z;
public:
Test() : {int x = _COUNTER_},{int y = _COUNTER_},{int z = _COUNTER_} { }
x = 0;;
y = 0;;
z = 0;;
};
可能不是预期的结果;-)
也许您想要类似的东西:
#define ABC(name) class name {int _DATE_;
#define AAA public
#define INIT(number) number(_COUNTER_)
#define ZERO 0
#define CREATE(number) int number = ZERO;
ABC (Test)
CREATE(x)
CREATE(y)
CREATE(z)
AAA:
Test(int _COUNTER_) : INIT(x),INIT(y),INIT(z) { }
};
然后:
pi@raspberrypi:/tmp $ g++ -E m.cc
# 1 "m.cc"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "m.cc"
class Test {int _DATE_;
int x = 0;
int y = 0;
int z = 0;
public:
Test(int _COUNTER_) : x(_COUNTER_),y(_COUNTER_),z(_COUNTER_) { }
};
如此缩进:
class Test {
int _DATE_;
int x = 0;
int y = 0;
int z = 0;
public:
Test(int _COUNTER_) : x(_COUNTER_),y(_COUNTER_),z(_COUNTER_) { }
};