Hi all,

I have a switch statement with more than 15 different cases. I want to replace that with any better alternative which would be efficient and also will take less code.

I came to know that function pointer can be one alternative. I dont know any anything about and had not used these till now.

Please let me know how it can be done or any better alternate if available.


Thanks in advance.

Recommended Answers

All 5 Replies

You could use an array of function pointers and the index into the array could be the case values, for example

typedef struct 
{
   int (*fn)();
}fns;

int f1() {return 0;}
int f2() {return 0;}
int f3() {return 0;}
int f4() {return 0;}
int f5() {return 0;}
int f6() {return 0;}
int f7() {return 0;}
int f8() {return 0;}
int f9() {return 0;}
int f10() {return 0;}
int f11() {return 0;}
int f12() {return 0;}
int f13() {return 0;}
int f14() {return 0;}
int f15() {return 0;}

fns ptrs[] = {f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15};

int main()
{
    int something = 5;
    ptrs[something].fn(); // call the function pointer
}

There will likely be the same amount of code in any structure you devise. Te difference is that it will be stored in various arrangements so as to not ugly up a single function scope.
If a switch is what your program calls for it is probably better to stay with that - hard to tell from what your post.
If you do need to switch to function pointers you can either use a functor (class that implements operator() ) or a standard function pointer.
A function pointer is implemented as follows:

#include <iostream>

void fun () { std::cout << "fun() called\n"; }

// Declare a type that points to a function taking no arguments 
// and returning void
typedef void (*fun_ptr)();

int main () {

    fun_ptr caller = fun; // assign function pointer to your function
    fun(); // invoke function through pointer
    return 0;
}

The syntax for that is a little hard to work with at first. Googling C++ function pointer declaration should get you started.

Ok.... Got the Idea. Thanks Ancient Dragon and L7Sqr.

But what if some of my cases are not big enough to put it into separate functions. Just a thought!

Again, this is hard to describe in the absence of an example.

Suppose the following:

switch (val) {
   case VAL1:
      printf("VAL1\n");
      break;
   case VAL2: case VAL3: case VAL4:
      break; /* no-op */
   case VAL5:
      {
         int x = 3;
         // some extended scoped operations here
      }
      break;
   default:
      return NULL;
}

Like I mentioned originally, this really doesn't warrant not using a switch statement. All I would change is that I would make the body of the VAL5 case a function call instead of a nested scope. In fact, in general, a nested scope in a switch usually indicates a good opportunity to move to a function call.

Of course, you could always just call an empty function for each of the unused cases - nothing is too small to fit into a function scope.

More direct answers could be provided if you gave an example of what you are trying to do and why you consider a switch statement to be a poor approach.

If you have a uniform function or can make one, then you could map it.

std::map< int , void(*)()> myMap;
const int ON_UPDATE = 0;
const int ON_QUIT = -1;

myMap[ON_UPDATE] = updateHandler;
myMap[ON_QUIT] = quitHandler;

while( ! someCondition ){
 int state = getState();
 myMap[ state ] (); //call function
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.