Search code examples
c++classmethodsgetset

Generate set/get methods for a c++ class


Is there any tool that generates set and get methods for a class automatically.

Just I create classes very frequently and would like to have a tool which for each class-member wil generate the following functions automatically:

Member_Type getMemberName() const; //in header file
Member_Type getMemberName() const //in source file 
{
    return member;
}

void setMemberName(const Member_Type & val); //in header
void setMemberName(const Member_Type & val) //in source file 
{
    member = val;
}

I have met a macro like this but did not like the idea:

#define GETSETVAR(type, name) \
private: \
    type name; \
public: \
    const type& get##name##() const { return name; } \
    void set##name##(const type& newval) { name = newval; }

May be someone knows how to do that with MS Visual Studio, or eny other tool?


Solution

  • Not the tool actually, but you could use Encapsulate Method in Visual Assist X, for example, which makes getter / setter methods for some private class member.

    Sure, many of tools that work similiar as VAX do have the same methods.

    Also, if you have to do this action for a huge amount of classes, you could implement your own command-line tool and lauch it for every source file that you have.