在 Visual C++ 中生成访问器2008年
我试图自动为我的变量生成访问器和修改器,但找不到方法。我尝试了右键单击/重构...解决方案,但重构项没有出现。
我现在没有心情学习如何编写宏来执行此操作,而且我没有钱购买商业解决方案(实习生)。 欢迎任何帮助,我不想用手写所有的获取/设置。
注意:它是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,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.
我知道为所有成员变量编写 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.