473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Design issue: pointer to member function VS switch VS other solutions

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ò
Jan 7 '07 #1
4 1563
* Luigi Malagò:
people *p = new people();
people.setAddMan();
parser->parse("man.txt",people);
people.setAddWoman();
parser->parse("woman.txt",people);
People people;
parser->parse( "man.txt", people.men() );
parser->parse( "women.txt", people.women() );

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 7 '07 #2
Alf P. Steinbach ha scritto:
* Luigi Malagò:
>people *p = new people();
people.setAddMan();
parser->parse("man.txt",people);
people.setAddWoman();
parser->parse("woman.txt",people);

People people;
parser->parse( "man.txt", people.men() );
parser->parse( "women.txt", people.women() );
Are you passing to the parser the collection where to add the objects,
instaed of the whole container (of both men and women)?
Could you explain it a little bit further your solution?

thanks a lot,
Luigi
Jan 7 '07 #3
Luigi Malagò ha scritto:
Alf P. Steinbach ha scritto:
>* Luigi Malagò:
>>people *p = new people();
people.setAddMan();
parser->parse("man.txt",people);
people.setAddWoman();
parser->parse("woman.txt",people);

People people;
parser->parse( "man.txt", people.men() );
parser->parse( "women.txt", people.women() );

Are you passing to the parser the collection where to add the objects,
instaed of the whole container (of both men and women)?
Could you explain it a little bit further your solution?

thanks a lot,
Luigi
I forget to mention that to me (in my application) men and women are the
same, i mena, i never thought to create sublasses of person since the do
not differ at all. The only difference is to with collection inside
people the are added.

Another important think is that in case a pass as function pointers the
members i want the parser to use to add the object t people, i would
have to pass both "addPerson" (pointing to addMan or addWoman) but also
another method (at least one, i think) since before adding the object to
the collection i have to add to is some information that i can get from
the object people. For doing that i have to call getPets(string name)
and then add what i get to the person p. In my application the list of
pets depends on whether person is a man or a woman. So i thought about
having another pointer getPets that refers to getPetsForMan(string name)
ot getPetsForWoman(string name).

The whole is getting a little bit complicated and now i doubt about my
choice..

NOTE: I know this example is a little bit sutpid, the real application
is more complex and i'd like to focus on the problem rather then the
real example..

Luigi
Jan 7 '07 #4
Luigi Malagò wrote:
>
Are you passing to the parser the collection where to add the objects,
instaed of the whole container (of both men and women)?
Could you explain it a little bit further your solution?

I forget to mention that to me (in my application) men and women are the
same, i mena, i never thought to create sublasses of person since the do
not differ at all. The only difference is to with collection inside
people the are added.
Do you have any suggestion to improve my design?
One of the flexible ways, if you want to divide people
in future more, to do like this:

declare parser's storage class interface
{
};

At this point no sence to make "people" as derived from "storage",
because "people" contains more than one "storage"

You will be forced to pass people to parser if only parser will access
to all man/woman/children together, but simultaneously will have any
preffered selected type of storage (man for example).

declare people class interface
{
storage& man();
storage& woman();
storage& other();
};

declare parser class interface
{
parser::parse(file&,storage&);
};

Usage

do_parse(Tpeople *people, Tparser *parser)
{
parser->parse("man.txt",people->Man() );
parser->parse("woman.txt",people->Woman() );
parser->parse("children.txt",people->other() );
parser->parse("old.txt",people->other() );
}

Tpeople *people = new Tpeople;

do_parse(people, new Tlex_parser);
do_parse(people, new Tbison_parser);

Not enough information to apply desing patterns, but all looks like
simplest kind of design pattern "builder" with "storage" as "concrete
builder without derived" and "parser" as "concrete derived from
director".

Jan 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: xsist10 | last post by:
whats the best method. delphi lets you do something very simple ptrMethodFoo : procedure how do you do this in C++?
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
6
by: Jacek Dziedzic | last post by:
Hello! First of all please forgive me for not posting a compilable snippet, but rather a simplified piece of code with the unimportant details left out. Let's say I have two classes...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
2
by: nw | last post by:
Hi, I previously posted a question asking for suggestions on my class design. My original code consisted of a pure virtual base class Body which contained a number of integration algorithms...
4
weaknessforcats
by: weaknessforcats | last post by:
Design Patterns – State Often computer software operates based on a condition called a state. These states traditionally have been implemented using a switch statement. The cases of the switch...
5
by: Fei Liu | last post by:
Hello, I have a situation where I need to design a library for multi-thread application, each thread does some work in a client supplied std::ptr_fun(free_function) or a functor. Now since it's...
31
by: huili80 | last post by:
Say I have two classes: class A { public: int x; }; class B {
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.