473,569 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.setAddMa n();
parser->parse("man.txt ",people);
people.setAddWo man();
parser->parse("woman.t xt",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(perso n 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 1574
* Luigi Malagò:
people *p = new people();
people.setAddMa n();
parser->parse("man.txt ",people);
people.setAddWo man();
parser->parse("woman.t xt",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.setAddM an();
parser->parse("man.txt ",people);
people.setAddW oman();
parser->parse("woman.t xt",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.setAdd Man();
parser->parse("man.txt ",people);
people.setAdd Woman();
parser->parse("woman.t xt",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(s tring 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(f ile&,storage&);
};

Usage

do_parse(Tpeopl e *people, Tparser *parser)
{
parser->parse("man.txt ",people->Man() );
parser->parse("woman.t xt",people->Woman() );
parser->parse("childre n.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
7918
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
4952
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 signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well...
6
2441
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 'box_shape' and 'cylinder_shape' that derive from a common base class 'shape', more or less like this: namespace Shapes {
0
2496
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 don't work nearly as well as they should, even for analysts and power users. The reason they haven't reached the masses is because most of the tools...
2
1792
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 which are applied to the Body. Generally only one integration algorithm is used via an integrate() method, which selected the integration algorithm to...
4
14710
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 represent the various states. The example below might be used to read a disc file int state = 1; switch (state) { case 1: //open...
5
2522
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 a multi-threaded application, naturally I want the call back functor to be created on a per thread basis. Suppose my thread is so defined ...
31
1952
by: huili80 | last post by:
Say I have two classes: class A { public: int x; }; class B {
4
4387
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 pointer array instead of switch. They believe that ordinary function pointer is much faster than switch. Think of one variable called...
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.