在 Visual C++ 中生成访问器2008年

发布于 2024-08-27 09:09:24 字数 250 浏览 10 评论 0原文

我试图自动为我的变量生成访问器和修改器,但找不到方法。我尝试了右键单击/重构...解决方案,但重构项没有出现。

我现在没有心情学习如何编写宏来执行此操作,而且我没有钱购买商业解决方案(实习生)。 欢迎任何帮助,我不想用手写所有的获取/设置。

注意:它是 C++,所以 {get;set} 语法不起作用(或者起作用?)。

编辑:这不是关于“为所有变量生成读/写访问器”。我希望能够生成一堆 Get 访问器,而不必为每个访问器编写原型和声明。

I'm trying to generate the accessors and mutators for my variables automatically, but just can't find a way. I tried the right-click/refactor... solution, but the refactor item doesn't appear.

I'm not in the mood right now to learn how to write a macro to do this, and I don't have the money to buy a commercial solution (internship student).
Any help is welcome, I don't feel like writing all my get/set by hand.

note : it's C++ so the {get;set} syntax doesn't work (or does it ?).

edit : it's not about "generate a read/write accessor for all variables". I want to be able to generate a bunch of Get accessor without having to write the prototype and declaration for each of them.

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

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

发布评论

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

评论(2

回忆追雨的时光 2024-09-03 09:09:24

不,C++ 没有访问器语法。 C++ 程序员对该语言未很好支持的功能感到不满。也没有很多 Resharper 风格的工具。如果您不喜欢打字,那么 C++ 不是您应该考虑的语言。

让 Neil 开心,并通过省略“get”前缀来避免“糟糕的设计”的贬低。类似于 size(),而不是 getSize()。

MSVC 支持使用 __declspec(property) 声明符声明属性。然而,这是非常不标准的。并且需要大量输入,您仍然需要编写访问器函数。

No, C++ doesn't have syntax for accessors. C++ programmers frown on features that are not well supported by the language. Nor does if have many Resharper style tools. If you don't like to type then C++ is not a language you should consider.

Keep Neil happy and avoid the "bad design" put-down by omitting the "get" prefix. Like size(), not getSize().

MSVC supports declaring properties with the __declspec(property) declarator. It is however very non-standard. And takes a lot of typing, you still need to write the accessor functions.

超可爱的懒熊 2024-09-03 09:09:24

我知道为所有成员变量编写 getter/setter 在某些语言中很流行,这些语言的拥护者声称它们比 C++ 更加面向对象,但在我看来,如果你这样做,那么这根本就不是面向对象。

请阅读这篇论文,它比我以前更雄辩地解释了我对这种实践的看法。

I know writing getters/setters for all the member variables is en vogue in some languages whose advocates claim them to be much more OO than C++, but IMO if you do so, then this is not OO at all.

Please read this paper, which explains what I think about this practice much more eloquently than I ever could.

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