switch-case via function pointers...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rahul

    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 ! ! !
  • Paavo Helde

    #2
    Re: switch-case via function pointers...

    Rahul <sam_cit@yahoo. co.inwrote in news:b1def093-26dc-4e23-946a-
    feab54e0caa3@s1 9g2000prg.googl egroups.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

    Comment

    • Rahul

      #3
      Re: switch-case via function pointers...

      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,



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

      Comment

      Working...