Hello,
i'm new to function pointers. I have some code that parses some
configuration files using flex and bison. I have to use the same parse
for different files, but the information i parse have to be add to
different collections. I have a C++ class that wraps the code that
invoce the parser. I pass to the parser an object that works as a
container for the different object i have to parse.
Let make an example. I have to parse men.txt and woman.txt. I have a
class People (a container for men and women, where i want to keep them
in different lists), with different methods: addMan(person p) and
addWoman(person p). The parser creates person p and then invokes a
method add(person p) that will call addWoman or addMan according to what
the user decide.
people *p = new people();
people.setAddMan();
parser->parse("man.txt",people);
people.setAddWoman();
parser->parse("woman.txt",people);
In this way the invocation of the method parse is the same for every
type of file i parse (men or women), and so i do not have to maintain
more versions of flex and bison files...(note taht a good idea would be
to create e hierarchy of parsers, but since i use flex and bison, this
is not possible, i would have to duplicate all the code for the parsers..)
I can change the behavior of add(person p) simply using a switch and a
bool variable, or otherwise using pointer to member functions. The first
question is: which is the best solution? In this case are pointer to
functions worth using?
Besides that, i'm not sure i'm following the best approach...
Do you have any suggestion to improve my design? I though about having
man and woman class, but this would mean to have different files for
bison ( i would have to create two parsers), since the parser has to
create different objects according to the file parsed. (otherwise i
could use a boolean variable into the parser, but i dont like this idea
since it would make my parse dependent on the type on the objects of the
collection...)
Another option would be pass to the parser dinamically at runtime the
type of object to create (man or woman), and then invoke always the same
method addPeople(person p), and than ckeck dinamically what kind of
object has been passed.
I hve many ideas in my mind, but i'd like hear from you what do you
think is a nice design.
thanks a lot,
Luigi Malagò