Is there a way to automatically generate getters and setters if they aren't present in C++?
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 presen开发者_运维技巧t (@synthesize
).
Is there a way to do this in C++, or do I need to implement all getters and setters myself?
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:
- Visual Studio Code:
- Getter and Setter Generator
- Getter/Setter Generator
- Vim
- vim-refactor
- Emacs
- semantic-refactor
- Visual Studio
- Resharper C++
- Visual Assist
- GS Assist
- CLion
- built in
- Eclipse
- built into Exclipse CDT (“Implement method”)
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
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.
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..
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
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
精彩评论