If I have many private Data members in class... is there any other way of setting and getting instead of declaring setter and getter individually? If so please help. Thankyou
Asked
Active
Viewed 40 times
-1
-
You can always make them public – ForceBru Feb 25 '16 at 12:13
-
2What are you asking here? Are you looking for a way to get automatically-generated getter/setter functions? – Cody Gray - on strike Feb 25 '16 at 12:15
-
Please see [Are getters and setters poor design?](http://stackoverflow.com/questions/565095/are-getters-and-setters-poor-design-contradictory-advice-seen) – Bo Persson Feb 25 '16 at 12:21
2 Answers
0
This is a really common pattern: It is considered best practice is to have set and get functions instead of making a variable public, even if all the accessors do is modify or return the value. C++ does not have anything built into the language to reduce the amount of typing needed.
Depending upon your needs, one option is to use a third party library. For example, Qt provides Q_PROPERTY which provides what you ask for plus quite a bit more, like serialization. That type of class is not appropriate for a small, efficient C++ class, but it is a good option for large classes with lots of members (such as a class with all a member for each entry in a dialog box).

Rob L
- 2,351
- 13
- 23
-
Qt's `Q_PROPERTY` mechanism doesn't really have anything to do with C++. It uses a separate meta-object compiler. So suggesting it as merely a "third party library" is a little bit misleading, I think. As an extra-linguistic mechanism, it is far more than that. – Cody Gray - on strike Feb 25 '16 at 12:44
-
Yes. To clarify, my point was exactly that: depending on the situation, extra-linguistic features might be a possibility. However, if one of my coders said "I don't need Qt, but I added it to this project just so I don't have to write accessors" then they'd probably get fired..... ;) – Rob L Feb 25 '16 at 14:56