1.非指针类型的成员变量的生成
- // PropertyBuilderByName 用于生成类的成员变量
- // 并生成set和get方法
- // type 为变量类型
- // access_permission 为变量的访问权限(public, priavte, protected)
- #define PropertyBuilderByName(type, name, access_permission)\
- access_permission:\
- type m_##name;\
- public:\
- inline void set##name(type v) {\
- m_##name = v;\
- }\
- inline type get##name() {\
- return m_##name;\
- }\
该宏生成成员变量m_name,具体有name决定,其访问权限由acess_permission指定
2.指针类型的成员变量的生成
- #define PointerPropertyBuilderByName(type, name, access_permission)\
- access_permission:\
- type* m_##name;\
- public:\
- inline void set##name(type* v){\
- m_##name = v;\
- }\
- inline type* get##name(){\
- return m_##name;\
- }\
3.使用实例
- class Test
- {
- PropertyBuilderByName(int, A, private)
- PointerPropertyBuilderByName(double, DBV, private)
- };
- int main()
- {
- Test t;
- t.setA(10);
- cout << "A = " << t.getA() << endl;
- double a = 12.3445;
-
- t.setDBV(&a);
- cout << "DBV: " << *(t.getDBV()) << endl;
- system("pause");
- return 0;
- }
阅读(4911) | 评论(1) | 转发(1) |