为 c++ 生成 set/get 方法班级

发布于 2024-09-04 00:28:07 字数 680 浏览 1 评论 0原文

是否有任何工具可以自动生成类的 set 和 get 方法。

只是我非常频繁地创建类,并且希望有一个工具可以为每个类成员自动生成以下函数:

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

我遇到过这样的宏,但不喜欢这个想法:

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

可能有人知道如何用 MS 做到这一点Visual Studio,或者其他工具?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凶凌 2024-09-11 00:28:07

实际上不是工具,但您可以在 Visual Assist X 中使用封装方法,例如,它为某些私有类创建 getter / setter 方法成员。

当然,许多与 VAX 类似的工具都有相同的方法。

此外,如果您必须对大量类执行此操作,您可以实现自己的命令行工具并启动它适用于您拥有的每个源文件。

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.

暖伴 2024-09-11 00:28:07

为什么要生成这些方法?

一般来说,更好的方法是创建一个实际使用接口来定义对象上的操作,而不仅仅是单个属性集。通过创建类接口,您无需编写一堆 getter 和 setter。

如果您确实需要为所有(甚至大多数)属性生成公共 getter 和 setter,可能更好的方法是将其设为结构体,那么就不需要这样的生成。

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.

゛时过境迁 2024-09-11 00:28:07

如果您使用的是 Visual Studio,我的 Atomineer Pro Documentation 插件将执行此操作 - 它会立即添加成员字段的 getter/setter 方法,使用您选择的命名样式。

例如,如果你有这个:

public:

   ...

private:
    int m_Speed;

那么你可以执行命令将其转换为这个

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

    ...

private:
    int m_Speed;

(注意:你不必使用“m_”或“Get...” - 这只是一个示例来展示它如何处理前缀或后缀命名方案,您可以配置使用的成员命名样式(speed、_speed、mSpeed、m_speed 等)和 getter/setter 方法的命名样式(GetSpeed()、get_speed() 等)


当应用于 C# 成员时,它会执行类似的操作:

protected int m_Speed;

然后您可以执行命令将其转换为自动属性:

protected int Speed { get; set; }

...并再次执行该命令以生成带有支持字段的属性:

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

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;
呢古 2024-09-11 00:28:07

同意 Kotti - Visual Assist(和其他插件)提供此功能。

实际的源代码应该具有 getter/setter,因为您可能希望根据需要向 setter 添加验证和更改通知。

使用宏来实现这一点只是……可打脸。 (愿意根据要求详细说明)。

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).

迟月 2024-09-11 00:28:07

我找不到工具,所以我在 10 分钟内写了一个 python 脚本。它并不完美,但很有帮助:

generate_getters.py:

#!/usr/bin/python
import sys

if len(sys.argv) < 1:
    print 'usage: > python script.py <cpp_file_name>'

fileName = sys.argv[1]
className = fileName.split('.')[-2].split("/")[-1]

print 'classname:' +  className

def printGetterSettersForLine(line):
    syntax = line.strip().split(';')[0]
    words = syntax.split()
    if len(words) != 2:
        return
    getter = words[0] + ' ' + className + '::get' + words[1].title() + '() { \n'
    getter = getter + '     return ' + words[1] + ';\n'
    getter = getter + '}';
    print getter

with open(fileName) as f:
        lines = f.readlines()
        for line in lines:
            printGetterSettersForLine(line)

用法:> pythongenerate_getters.py 类名.h
生成设置器留给读者作为练习。 ;)

I could not find a tool so I wrote a python script in 10 minutes. Its not perfect but its helpful:

generate_getters.py :

#!/usr/bin/python
import sys

if len(sys.argv) < 1:
    print 'usage: > python script.py <cpp_file_name>'

fileName = sys.argv[1]
className = fileName.split('.')[-2].split("/")[-1]

print 'classname:' +  className

def printGetterSettersForLine(line):
    syntax = line.strip().split(';')[0]
    words = syntax.split()
    if len(words) != 2:
        return
    getter = words[0] + ' ' + className + '::get' + words[1].title() + '() { \n'
    getter = getter + '     return ' + words[1] + ';\n'
    getter = getter + '}';
    print getter

with open(fileName) as f:
        lines = f.readlines()
        for line in lines:
            printGetterSettersForLine(line)

Usage: > python generate_getters.py ClassName.h
Generating setter is left as exercise for reader. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文