James Kanze
10/7/2008 11:58:00 AM
On Oct 6, 6:00 pm, Hendrik Schober <spamt...@gmx.de> wrote:
> Sarath wrote:
> > I've a class which has lot of Get/Set functions. Since most
> > of the data members are of same type (e.g float), I'd like
> > to implement it with the help of lookup table ( probably
> > will be using stl maps). The code size will be considerably
> > reduced if I use this method(by avoiding set/get functions
> > for each members). [...]
> For one, this raises the question of why the class exists
> in the first place. A class should support a certain
> abstraction, and having all data fields effectively public
> (which is what getters/setters usually come down to) doesn't
> seem to come with a notable level of abstraction.
> OTOH, if you don't want to revisit your design, make all
> getters and setters 'inline'. That will keep the syntax the
> way it is while effectively erasing the getters/setters
> from the resulting executable.
Some classes are mainly data recepiants; we've got similar
classes representing the data in the data basae, for example.
There are two "classical" solutions for this. The most general
is simply to use automatically generated code, and not worry
about the similarity of all of the functions. This has the
advantage that you can easily make them all virtual, and derive
and "intercept" certain setters, e.g. so that setting specific
values triggers other actions. The other alternative is to make
the entire thing dynamic, basically using something like
std::map< std::string, boost::variant< supported types > > as
the entire implementation, and looking up each variable by name.
It's also possible to mix the two solutions, using the generated
code for the actual data, but with an std::map< std::string,
Accessor* > to support symbolic access as well. (Obviously,
that map would be static, and also automatically generated.
This solution becomes very interesting when you start throwing
in additional functionality like serialization or reading and
writing from a data base using SQL.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34