问题描述
我最近看到了一个很棒的c风格宏播放,该播放会自动为该类生成设置器/获取器.这就是我在说的.
I have recently seen a cool c style macro play which generates automatically the setters/getters for the class. So this is what I am talking about.
#define BOOL_VARIABLE(name)\
void set##name(bool iValue)\
{\
// set the boolean
}\
const bool get##name() const\
{\
// get the boolean
}
BOOL_VARIABLE(AVariableName); // and calling them inside the class
- 现在想想所有的串/INT/双等变量其中之一
我知道所有避免使用宏的类型,但实际上我觉得它很酷,因为较长的getter/setter语句确实打扰了我.您能想到这种方法有什么问题吗?
I am aware of all avoid-macro usage type of remarks but I actually find it pretty cool because long getter/setter lines are really disturbing me. Can you think of anything that might go wrong with this approach.
推荐答案
以下*是更好的选择(类型也作为参数).不过,我会避免实施采取的存取方法充分利用,如@justin提及.如果您忘记实现它们,编译器将生成错误.
The following* is a better option (with type also as a parameter). However, I would avoid implementations to take full advantage of accessor methods, as mentioned by @justin. Your compiler will generate errors if you forget to implement them.
* http://cppkid.wordpress.com/2008/09/04/getters-and-setters-for-a-class-using-a-macro/
#define GETSET(type, var) \
private: \
type _##var; \
public: \
type Get##var() \
{\
return _##var; \
}\
void Set##var(type val) \
{\
_##var = val; \
}
这篇关于吸气/setter产生与多行宏stringfy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!