|
Macro For Accessors
Submitted by |
This is my first contribution to the TOTD.
For those of you who are using C++, you have certainly a large amount of private
(protected) member variables that should be accessed from the outside of the
class. The answer is writing accessors, but typing them again and again can be
annoying.
So here is this little macro:
#define MAKEACCESS(type,prefix,variable) \
public: \
inline type get_##variable (){return m_##prefix##variable ; } \
inline void set_##variable (type _##prefix##variable){ m_##prefix##variable = _##prefix##variable ;}\
protected: //Or private, as you wish. |
This can be used as this:
class CmyClass
{
public:
CmyClass();
protected:
int m_iVar;
MAKEACCESS(int,i,Var);
} |
Im using prefixes, so I included them in the macro, but it is easily
customizable. Another point is that the declaration is not included in the
macro, because I think this is more readable.
For those who wants the declaration inside the macro:
#define MAKEACCESS(type,prefix,variable) \
public: \
inline type get_##variable (){return m_##prefix##variable ; } \
inline void set_##variable (type _##prefix##variable){ m_##prefix##variable = _##prefix##variable ;}\
protected: \
type m_##prefix##variable ; |
I hope this will be useful.
Cheers,
Slurdge
|
The zip file viewer built into the Developer Toolbox made use
of the zlib library, as well as the zlibdll source additions.
|