468,512 Members | 1,429 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

switch-case via function pointers...

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 ! ! !
Mar 5 '08 #1
2 3811
Rahul <sa*****@yahoo.co.inwrote in news:b1def093-26dc-4e23-946a-
fe**********@s19g2000prg.googlegroups.com:
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
Mar 5 '08 #2
On Mar 5, 12:33 pm, Rahul <sam_...@yahoo.co.inwrote:
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-lit...html#faq-33.10

But yes, this can work only with c++ ;-) not with c :-(
Mar 18 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Bryan Parkoff | last post: by
12 posts views Thread by junky_fellow | last post: by
13 posts views Thread by William Stacey | last post: by
18 posts views Thread by Torben Laursen | last post: by
2 posts views Thread by 7777777.chen | last post: by
13 posts views Thread by Michael Griebe | last post: by
25 posts views Thread by v4vijayakumar | last post: by
11 posts views Thread by Peter Kirk | last post: by
11 posts views Thread by =?Utf-8?B?anAybXNmdA==?= | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.