Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #67854
| From | Bo Persson <bo@bo-persson.se> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: About getters/setters |
| Date | 2019-08-21 21:40 +0200 |
| Message-ID | <gs5l26F48alU1@mid.individual.net> (permalink) |
| References | <kSg7F.32840$Xs5.970@fx06.am4> |
On 2019-08-21 at 21:22, JiiPee wrote:
> Which one is better:
>
> 1)
>
> class Person
>
> {
>
> public:
>
> int getAge() { return age;}
>
> void setAge(int a) { age = a;}
>
> private:
>
> int age;
>
> };
>
> or
>
> 2)
>
> struct Person
>
> {
>
> int age;
>
> };
>
> ####
>
> I know Herb Sutter recommended 1) but for example isocpp
>
> https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-get
>
> seems to recommend 2). Herb says that for example when debugging 1) is
> good because you can write to file all the accesses of the age, or cout
> all the calls. Also debugger can stop at the function call, but can
> debugger stop at :
>
> person.age = 6;
>
> ?
>
> So debugger can put a red dot to setAge and see which object changed it.
> And cout/write on file information.
>
> Argument also, that later on can add other functionality punder setAge
> (like cache logic).
>
> But 2) people argue its so simple and not likely to change, so it breaks
> OO idea.
>
> I do currently 1) but wondering which way to go.
>
> Like now I have a "Settings" class. Simple data members. I started
> adding getters and setters....
>
There probably isn't a general answer, except that it depends.
If you model a real person, the age doesn't change except by +1 on each
birthday. So what is the use for setAge?
In the Core Guidelines you have a Point(x, y) where it is unclear what
the use for set_x is. So you could just skip it.
If the class were to have some kind of behavior, perhaps a
Point.move_to(somewhere) would be more useful?
So, it depends. :-)
Bo Persson
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
About getters/setters JiiPee <no@notvalid.com> - 2019-08-21 20:22 +0100
Re: About getters/setters Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-08-21 19:34 +0000
Re: About getters/setters Bo Persson <bo@bo-persson.se> - 2019-08-21 21:40 +0200
Re: About getters/setters Öö Tiib <ootiib@hot.ee> - 2019-08-21 12:52 -0700
Re: About getters/setters JiiPee <no@notvalid.com> - 2019-08-22 15:25 +0100
Re: About getters/setters Öö Tiib <ootiib@hot.ee> - 2019-08-22 09:06 -0700
Re: About getters/setters Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-08-24 04:32 -0700
Re: About getters/setters Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-08-22 15:09 -0700
Re: About getters/setters Ian Collins <ian-news@hotmail.com> - 2019-08-23 16:17 +1200
Re: About getters/setters Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-08-23 05:26 -0700
Re: About getters/setters Melzzzzz <Melzzzzz@zzzzz.com> - 2019-08-23 18:07 +0000
Re: About getters/setters JiiPee <no@notvalid.com> - 2019-08-23 07:09 +0100
Re: About getters/setters JiiPee <no@notvalid.com> - 2019-08-23 07:13 +0100
Re: About getters/setters David Brown <david.brown@hesbynett.no> - 2019-08-23 09:30 +0200
Re: About getters/setters Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-08-25 06:16 +0000
Re: About getters/setters David Brown <david.brown@hesbynett.no> - 2019-08-25 12:12 +0200
Re: About getters/setters Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-08-23 11:24 +0000
Re: About getters/setters James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-08-23 08:34 -0400
Re: About getters/setters Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-08-23 14:48 +0000
Re: About getters/setters Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-08-23 15:49 +0000
csiph-web