• Coding
  • pointers to random function c++

I don't know if the title is clear, but here's the problem.

I doing a small game/rendering engine for my 3D programming class and I have to implement basic controls to enable/disable certain features : different types of lighting, shadow, normal mapping, controlling an avatar, camera, ... and to edit the terrain I made a basic terrain editor where i can insert objects, alter the heightmap, .... . So i have 2 controll modes (normal and sceen editor) and stuff to controll.
I started managing them with a nasty block of switch than had to make an if else but it is annoying me. It is so ugly to in see that big if else code and it is getting annoying to maintain and change the controls as more stuff are being added.

so my idea is to make a simple structure containing :
- the control name
- vector of pointers to function that should be executed.
and then with a simple loop i can remove that ugly block.

the problem is the function does not have the same number of parameters or the same type. so for example making a vector of (void)(*func)(int) won't work.
so is there anyway to make a pointer to a random function in c++ ? or solve this problem in a different way ?
If it's annoying you, perhaps you just need to get used to it? How about refactoring different parts of the huge if/else or switch into different class methods.
Sometimes over designing something takes away time from actually making the app awesome.

Otherwise, it's a mistake to try and call different function signatures from the same location (using the same calling signature). It's as wrong as dividing by zero. It signifies that something is missing, or that you're expressing yourself wrongly.

This makes it necessary to ask what you're trying to do before trying to help with how to do it?
Don't be fooled, I am not saying that you're an idiot. On the contrary: (1) The problem you're facing is something others wouldn't have tried to solve in the first place. (2) You probably are facing limitations with the language itself.

What geek above linked to, especially the functor part has a significant little detail that you may be looking for. It's practically a valid technique to implement function currying. That's why it can handle the above semantical issue I talked about (signatures and so on). The functor itself handles passing the inherent function arguments, while the functor's caller gives any context necessary (if at all).
When you do have a large if/else or switch in a single function, you do have an "un-clean" code.
The general solution would be to consider inheritance as I think the end result is actually what you want.
the argument would be a reference to a class object for example if you are using the keyboard you could do something similar to:
class control{
    public:
      void apply(Keys &keys){} = 0;
}
class LightControl public control{
   private void toggleLights(void);
   public:
   void apply(Keys &keys){
      if(keys.isPressed(Keys.L)) toggleLights();
   }
}
Then you could have a ControlsManager static class where you just push controls:
ControlsManager.add(LightControl());
the ControlsManager has an update function that loops on all controls and calls apply every gameloop (sending the key presses).

The following advice is not for performance, it is just a clean code approach (http://books.google.com.lb/books/about/Clean_Code.html?id=dwSfGQAACAAJ&redir_esc=y);