Re: Why are Set & Get important?
On Jun 29, 9:53 am, Jorgen Grahn <grahn+n...@snipabacken.se> wrote:
On Sun, 28 Jun 2009 12:58:19 -0700 (PDT), James Kanze <james.ka...@gmail.=
com> wrote:
On Jun 28, 11:10 am, Ian Collins <ian-n...@hotmail.com> wrote:
Juha Nieminen wrote:
Immortal Nephi wrote:
Tens of member functions or hundreds of member functions
modify data members.
I'd say that if you have tens or even hundreds of accessor
functions, your class design is flawed.
Or even a couple!
It depends. The role of some classes is to represent
specific data. In such classes, almost all member functions
would be getters and setters (under some name or another).
At that point I often break down and call it a struct with
public data members (but with well-defined construction and
copy semantics).
I tend to call it a structure as well. But as far as C++ is
concerned, it is a class, even if I declare it with the keyword
'struct'.
One thing which helps a lot is if these data members are real,
distinct types. If I encounter a
struct Foo { int a, b, c; };
my first reaction is *not* to add set/get methods. Instead I
look at a, b and c and try to find out if they can't be made
something more type-safe than just an int. Maybe a and b
together really form a distinct type, and so on.
That's not a bad policy, in general. (It would be even better
if we has a variant of typedef which created a new type.) But
it's not always applicable, and in some cases, even if it could
be applicable, it would be overkill; you wouldn't really want
complex to be anything but two floating point types. And third
party interfaces have to be considered: the data base interface
will readily accept ints, but will probably bawk at your own
special type.
I think tiny, concrete types are underused in much C++ code.
Maybe too many programmers come from Java, where there is a
performance penalty for anything more complex than an int.
I agree with you there, but I won't go to the other extreme
either.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34