如果 C++ 中不存在 getter 和 setter,是否有办法自动生成它们?
我对 Objective-C 有丰富的经验,在 Objective-C 中,如果 getter 和 setter 尚不存在,您可以让编译器为您生成它们 (@synthesize
)。
有没有办法在 C++ 中做到这一点,或者我需要自己实现所有 getter 和 setter?
I'm experienced with Objective-C, and in Objective-C you can let the compiler generate getters and setters for you if they aren't already present (@synthesize
).
Is there a way to do this in C++, or do I need to implement all getters and setters myself?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
C++ 核心指南建议不要使用简单的 getter 和 setter 因为它们是不必要的,并且是不良面向对象的症状设计。因此,C++ 没有用于自动生成 getter 和 setter 的内置功能(尽管 元类,如果它们被包含在语言中,将使这成为可能)。
这与完善的软件工程原则相关告诉,不要问< /a>.
特别是,通过 setter 改变状态通常是代码异味和糟糕的架构设计的标志。此规则也有例外情况,纯粹出于实用性。这很好,但例外情况足够少,以至于它们不应该保证有工具来自动生成 getter 和 setter。
事实上,您可以将此作为试金石:每当您发现自己希望有一个工具来自动生成此类样板文件时,请退一步并重新考虑您的代码设计。
也就是说,有许多工具可以提供这些功能,并且从纯粹的实用角度来看,它们可能被证明是有用的,尽管我没有亲自测试过它们:
The C++ Core Guidelines advise against using trivial getters and setters because they’re unnecessary and a symptom of bad object-oriented design. As such, C++ has no built-in functionality for auto-generating getters and setters (though metaclasses, if they ever get included in the language, would make this possible).
This is related to the well-established software engineering principle tell, don’t ask.
In particular, mutating state via setters is usually a sign of code smell and a bad architectural design. There are exceptions from this rule, purely out of practicality. And this is fine, but the exceptions are few enough that they shouldn’t warrant tools to auto-generate getters and setters.
In fact, you may use this as a litmus test: whenever you find yourself wishing for a tool to autogenerate such boilerplate, take a step back and reconsider your code design.
That said, there exist a number of tools to provide the functionality and in purely practical terms they may prove useful, though I have not personally tested them:
不是编译器本身,而是像 eclipse CDT 这样的 IDE 实际上可以自动执行此操作(右键单击类 > 源 > 生成 Getters 和 Setters...)。
Not the compiler itself, but an IDE like eclipse CDT can actually perform this action automatcally (right click on class > Source > Generate Getters and Setters...).
你必须自己实现它们
You have to implement them yourself
没有办法做到这一点。但是,如果您使用像 Eclipse 这样功能齐全的 IDE(不确定 Visual Studio 是否具有此功能),则可以使用方便的选项让 IDE 为您生成 getter/setter 代码。
There is no way to do this. However, if you use a fully-featured IDE like Eclipse (not sure if Visual Studio has this feature), there are convenience options to have the IDE generate the getter/setter code for you.
对我来说,使用访问器(以 c# vb.net 的形式特别简单)的优点是
1/可以在 setter 或 getter 中设置断点,否则您无法知道谁以及何时有权访问该成员。
2/ 您可以控制设置什么值,以防止需要测试溢出或其他错误设置。这在公共库中特别需要,例如可视化组件的属性编辑器。
但这在您的私人内部类中并不完全需要。然后,我认为必须根据对象的最终公共使用来设置对成员的受控访问的选择。
The advantage for me of using accessors (specialy easy in the form of c# vb.net) is
1/ the possibility to set a break point in a setter or a getter else you cannot know who and when has access to the member.
2/ You can control what value is set in case of need to test overflow or other bad set. This is specially needed in a public libray, like a property editor for your visual component by example.
But this is not fully needed in your private internal class. Then the choice of a controlled access to a member must be set from the final public usage of your object I think.
不,编译器不会帮助你。例如,您可以创建一个宏,或在构建步骤中生成一些代码。
No, the compiler doesn't help you with that. You could for example create a MACRO, or generate some code in your build step..
我知道现在已经晚了,我正在使用 VS 2015,并且可以从 Visual Studio 市场安装一个插件/扩展,它可以让您方便地在此处生成 getter/setter 代码: https://marketplace.visualstudio.com/items?itemName=Gabsii.getter-setter-generator
I know this is late, I am using VS 2015, and there is a plugin/extension available to install from Visual Studio marketplace that will give you the convenience of generating getter/setter code here: https://marketplace.visualstudio.com/items?itemName=Gabsii.getter-setter-generator
至少在 Visual Studio 2015 中,我找不到它。但是,如果您使用 Resharper,那么它可以在 .h 文件中生成 setter 和 getter
At least in Visual Studio 2015, i can't find it. However, if you use Resharper then it could generates the setter and getter in the .h file