views:

147

answers:

2

Hi, 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.

+1  A: 

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.

sbi
+2  A: 

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.

Hans Passant
Thanks, that was an useful answer. I am aware of the methods like size() or length() and such, but I was just having fun learning how to deal with C++ classes. I'm working in embedded systems and I use only C, I know OO programming from Java and C#. Anyway, I just wanted to generate a few accessors for a project with two classes, so design considerations are pretty irrelevant.So, thanks again for the clear answer. PS : Don't tell me that "C++ is not C with classes", I'm aware of that already.
gramm