为 c++ 生成 set/get 方法班级
是否有任何工具可以自动生成类的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
实际上不是工具,但您可以在 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.
为什么要生成这些方法?
一般来说,更好的方法是创建一个实际使用接口来定义对象上的操作,而不仅仅是单个属性集。通过创建类接口,您无需编写一堆 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.
如果您使用的是 Visual Studio,我的 Atomineer Pro Documentation 插件将执行此操作 - 它会立即添加成员字段的 getter/setter 方法,使用您选择的命名样式。
例如,如果你有这个:
那么你可以执行命令将其转换为这个
(注意:你不必使用“m_”或“Get...” - 这只是一个示例来展示它如何处理前缀或后缀命名方案,您可以配置使用的成员命名样式(speed、_speed、mSpeed、m_speed 等)和 getter/setter 方法的命名样式(GetSpeed()、get_speed() 等)
当应用于 C# 成员时,它会执行类似的操作:
然后您可以执行命令将其转换为自动属性:
...并再次执行该命令以生成带有支持字段的属性:
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:
Then you can execute the command to convert it to this
(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:
then you can execute the command to convert it to an auto property:
...and execute the command a second time to produce a property with a backing field:
同意 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).
我找不到工具,所以我在 10 分钟内写了一个 python 脚本。它并不完美,但很有帮助:
generate_getters.py:
用法:> 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 :
Usage: > python generate_getters.py ClassName.h
Generating setter is left as exercise for reader. ;)