// file macroAid.hpp
#ifndef MACRO_AID_HPP
#define MACRO_AID_HPP
#define MACRO_SETTER_DECL( type, VarName)\
int Set##VarName(type v);
#define MACRO_SETTER_IMPL( cls, type, VarName, var)\
int cls::Set##VarName(type v) {\
this->var = v;\
return 0;\
}
#define MACRO_GETTER_DECL(type, VarName)\
type Get##VarName();
#define MACRO_GETTER_IMPL( cls, type, VarName, var)\
type cls::Get##VarName() {\
return this->var ;\
}
#define MACRO_VARIABLE_ACCESS_DECL( type, VarName, var )\
private:\
type var;\
public:\
MACRO_GETTER_DECL(type,VarName);\
MACRO_SETTER_DECL(type,VarName);
#define MACRO_VARIABLE_ACCESS_IMPL( cls, type, VarName, var)\
MACRO_SETTER_IMPL(cls, type, VarName, var);
MACRO_GETTER_IMPL(cls, type, VarName, var);
#endif
// a.hpp
#include "macroAid.hpp"
#ifndef A_HPP
#define A_HPP
class A { MACRO_VARIABLE_ACCESS_DECL(int, Size, size);
MACRO_VARIABLE_ACCESS_DECL(int, Age, age);
};
#endif
// aSetFunc.cpp
#include "a.hpp"
MACRO_VARIABLE_ACCESS_IMPL(A, int,Size, size);
MACRO_VARIABLE_ACCESS_IMPL(A, int,Age, age);
雖然 MACRO會讓程式碼不易讓程式碼分析器 例如:SOURCE INSIGHT 解析,
但它真的很方便可以減少打錯字的危機, 用簡單的幾行就可以完成原本複雜的工作
沒有留言:
張貼留言