Generate set/get methods for a c++ class

22,674

Solution 1

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.

Solution 2

Why do you want to be generating these methods?

Generally a better approach is to make an actual use interface that defines operations on the object, not just sets of individual attributes. By creating a class interface, you avoid the need to write a bunch of getters and setters.

If you really need to generate public getters and setters for all (or even most) of your attributes, probably better is to just make it a struct, then no such generation is needed.

Solution 3

If you're using Visual Studio, my Atomineer Pro Documentation add-in will do this - it will instantly add getter/setter methods for a member field, using a naming style of your choice.

e.g. if you have this:

public:

   ...

private:
    int m_Speed;

Then you can execute the command to convert it to this

public:
    // Access the Speed
    int GetSpeed(void) const    { return(m_Speed);  };
    void SetSpeed(int speed)    { m_Speed = speed;  };

    ...

private:
    int m_Speed;

(Note: you don't have to use "m_" or "Get..." - this is just an example to show how it handles prefixed or suffixed naming schemes. You can configure the member naming style used (speed, _speed, mSpeed, m_speed, etc) and the naming style for the getter/setter methods (GetSpeed(), get_speed(), etc))


It does similar things when applied to a C# member:

protected int m_Speed;

then you can execute the command to convert it to an auto property:

protected int Speed { get; set; }

...and execute the command a second time to produce a property with a backing field:

protected int Speed
{
    get { return(m_Speed); }
    set { m_Speed = value; }
}
private int m_Speed;

Solution 4

Agree with Kotti - Visual Assist (and other addins) provide this functionality.

The actual source code should have the getters / setters, because you probably want to add validation and change notification to the setters as needed.

Using a macro for that is just... facepunchable. (willing to elaborate on request).

Share:
22,674
Narek
Author by

Narek

Game developer.

Updated on June 24, 2020

Comments

  • Narek
    Narek almost 4 years

    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?