See what's going on with flipcode!




This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  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);
} 



I’m 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.

 

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.