如果 C++ 中不存在 getter 和 setter,有没有办法自动生成它们?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6912696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:52:21  来源:igfitidea点击:

Is there a way to automatically generate getters and setters if they aren't present in C++?

c++oopsettergetteraccessor

提问by Konrad Rudolph

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

我对 Objective-C 很有经验,在 Objective-C 中,如果 getter 和 setter 尚不存在,您可以让编译器为您生成它们 ( @synthesize)。

Is there a way to do this in C++, or do I need to implement all getters and setters myself?

有没有办法在 C++ 中做到这一点,还是我需要自己实现所有的 getter 和 setter?

回答by Konrad Rudolph

The C++ Core Guidelines advise against using trivial getters and settersbecause they're unnecessary and a symptom of bad object oriented design. As such, C++ has no builtin 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.

C++ 核心指南建议不要使用简单的 getter 和 setter,因为它们是不必要的,而且是糟糕的面向对象设计的症状。因此,C++ 没有用于自动生成 getter 和 setter 的内置功能(尽管元类,如果它们被包含在语言中,将使这成为可能)。这关系到完善的软件工程原则告诉,不要问

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.

特别是,通过 setter 改变状态通常是代码异味和糟糕的架构设计的标志。这条规则也有例外,纯粹是出于实用性。这很好,但例外情况很少,它们不应该保证自动生成 getter 和 setter 的工具。

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:

也就是说,有许多工具可以提供这些功能,从纯粹的实用角度来看,它们可能证明是有用的,尽管我没有亲自测试过它们:

回答by ascanio

Not the compiler itself, but an IDE like eclipse CDT can actuallyperform this action automatcally (right click on class > Source > Generate Getters and Setters...).

不是编译器本身,而是像 Eclipse CDT 这样的 IDE实际上可以自动执行此操作(右键单击 class > Source > Generate Getters and Setters...)。

回答by flumpb

You have to implement them yourself

你必须自己实现它们

回答by Eugene S

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.

没有办法做到这一点。但是,如果您使用功能齐全的 IDE,如 Eclipse(不确定 Visual Studio 是否具有此功能),则有一些方便的选项可以让 IDE 为您生成 getter/setter 代码。

回答by duedl0r

No, the compiler doesn't help you with that. You could for example create a MACRO, or generate some code in your build step..

不,编译器不会帮助你。例如,您可以创建一个宏,或在您的构建步骤中生成一些代码。

回答by duedl0r

The advantage for me of using accessors (specialy easy in the form of c# vb.net) is

我使用访问器的优势(特别是在 c# vb.net 的形式中很容易)是

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.

1/ 在 setter 或 getter 中设置断点的可能性,否则您无法知道谁以及何时可以访问该成员。

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.

2/ 您可以控制设置什么值,以防需要测试溢出或其他错误设置。这在公共库中是特别需要的,例如您的可视化组件的属性编辑器。

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.

但这在您的私人内部课程中并不完全需要。然后,我认为必须从对象的最终公共使用中设置对成员的受控访问的选择。

回答by Darrin

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

我知道这已经晚了,我正在使用 VS 2015,并且可以从 Visual Studio 市场安装一个插件/扩展,它可以让您方便地在此处生成 getter/setter 代码:https: //marketplace.visualstudio.com/ items?itemName=Gabsii.getter-setter-generator

回答by magallanes

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

至少在 Visual Studio 2015 中,我找不到它。但是,如果您使用 Resharper,则它可以在 .h 文件中生成 setter 和 getter