switch-case via function pointers...

R

Rahul

Hi,

I planning to replace the switch-case with an array of function
pointers, the index for the array would be the case integers. Each
case block would be implemented as a seperate function. It works fine
when all the functions have the exact signature like the return types
and formal input parameters.

In my case, some functions accept a different number of arguements.
Hence i can't have the array of different function pointers, is there
any alternative or work around to get it done by using the same
concept of invoking each case via function pointers?

Thanks in advance ! ! !
 
P

Paavo Helde

Hi,

I planning to replace the switch-case with an array of function
pointers, the index for the array would be the case integers. Each
case block would be implemented as a seperate function. It works fine
when all the functions have the exact signature like the return types
and formal input parameters.

In my case, some functions accept a different number of arguements.
Hence i can't have the array of different function pointers, is there
any alternative or work around to get it done by using the same
concept of invoking each case via function pointers?

Some options:

1. Create some wrapper functions so that all signatures are the same.

2. Put functions with different signatures into different arrays.

3. Define a union containing different types of function pointers and
make an array of unions. As in case 2, the call site must decide in
advance which signature to call.

hth
Paavo
 
J

Jeff Schwab

Rahul said:
Hi,

I planning to replace the switch-case with an array of function
pointers, the index for the array would be the case integers. Each
case block would be implemented as a seperate function. It works fine
when all the functions have the exact signature like the return types
and formal input parameters.

In my case, some functions accept a different number of arguements.
Hence i can't have the array of different function pointers, is there
any alternative or work around to get it done by using the same
concept of invoking each case via function pointers?

Thanks in advance ! ! !

If the functions are all callable using similar syntax, you could use
the TR1 "function" template.

http://www.devx.com/cplus/10MinuteSolution/32455/0/page/1

If there are minor variations in syntax, you may need another level of
compile-time indirection, e.g. to provide default values for unused
parameters.

long add2(int a, int b) { return long(a) + b; }
long add3(int a, int b, int c =0) { return long(a) + b + c; }

#include <cassert>
#include <vector>

#include <tr1/functional>

int main() {
std::vector<std::tr1::function<long (int, int)> > funcs;

funcs.push_back(add2);
funcs.push_back(add3);

assert(funcs.at(0)(3, 4) == funcs.at(1)(3,4));
}
 
R

Rahul

Hi,

I planning to replace the switch-case with an array of function
pointers, the index for the array would be the case integers. Each
case block would be implemented as a seperate function. It works fine
when all the functions have the exact signature like the return types
and formal input parameters.

In my case, some functions accept a different number of arguements.
Hence i can't have the array of different function pointers, is there
any alternative or work around to get it done by using the same
concept of invoking each case via function pointers?

Thanks in advance ! ! !

Just found a way to implement this. I can use functionoid concept to
implement an array of function pointers to functions of different
parameters,

http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.10

But yes, this can work only with c++ ;-) not with c :-(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top