我正在尝试使用C ++反射方法,并且在理解预处理器宏时遇到了问题。例如,以下代码有效。
header.h:
#define META_PROPERTY(NAME, TYPE, ACCESS, MIN, MAX) \
class NAME##_MetaProperty : public sge::GetSet<TYPE> \
{ \
public: \
NAME##_MetaProperty(TYPE *NAME) \
: GetSet(NAME, ACCESS) \
, min_(MIN) \
, max_(MAX) \
{} \
. . . other methods . . .
private: \
TYPE min_; \
TYPE max_; \
}NAME##_prop(&NAME); \
的main.cpp
main
{
uint32 u(255);
META_PROPERTY(u, uint32, ACCESS_DEFAULT, 0, 1024);
...
}
尽管构造函数不完整,宏仍然很乐意创建NAME ## _ MetaProperty对象,我想我明白了为什么因为预处理器只是填入MIN一个MAX参数。但是,如果我将构造函数更改为以下内容,则会出现一堆编译错误。
public: \
NAME##_MetaProperty(TYPE *NAME, TYPE MIN, TYPE MAX) \
: GetSet(NAME, ACCESS) \
, min_(MIN) \
, max_(MAX) \
{} \
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2143: syntax error: missing ')' before 'constant'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2143: syntax error: missing ';' before 'constant'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2059: syntax error: 'constant'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2059: syntax error: ')'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2334: unexpected token(s) preceding ':'; skipping apparent function body
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2059: syntax error: '&'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): warning C4183: 'u_prop': missing return type; assumed to be a member function returning 'int'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(45): error C2059: syntax error: '='
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(45): error C2238: unexpected token(s) preceding ';'
如果我预定义常量,我会得到另一组较小的错误。
main
{
const uint32 min(0);
const uint32 max(1024);
META_PROPERTY(u, uint32, ACCESS_DEFAULT, min, max);
}
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): error C2664: 'main::u_MetaProperty::u_MetaProperty(const main::u_MetaProperty &)': cannot convert argument 1 from 'uint32 *' to 'const main::u_MetaProperty &'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): note: Reason: cannot convert from 'uint32 *' to 'const main::u_MetaProperty'
1>d:\projects\sgesuite\tests\test04-reflection\src\main04.cpp(44): note: No constructor could take the source type, or constructor overload resolution was ambiguous
我只是想了解为什么我不能通过构造函数传递MIN和MAX。这个宏用我的常量做什么?