1

I'd want to know if it's possible to create a map with several function pointers members of an object.

Is this possible? This methods could have different return type or input arguments.

If not, (I suppose this reading a lot of related topics), I'd want the typical manner this can be done, for example in a GUI, if you change some parameter and then you have to call one setter or another, depending in what textbox you have changed.

Is there any design pattern to do this without using an giant if/else estructure?

Thanks in advance!

auroras
  • 51
  • 3

1 Answers1

2

Check it out my question here: Using a STL map of function pointers

The approach would be similar but instead that storing directly the function pointer you would have a struct

enum ArgumentType
{
  BOOL,
  INT,
  whatever
};

struct FunctionDecl
{
  void *function;
  ArgumentType returnType;
  ArgumentType arguments[];
}
Community
  • 1
  • 1
Jack
  • 131,802
  • 30
  • 241
  • 343
  • You can't (portably) store a function pointer in a data pointer. – ecatmur Jul 18 '12 at 15:44
  • 1
    And how do you call the functions? – R. Martinho Fernandes Jul 18 '12 at 15:47
  • Not portable but portability was not a requirement. To call them you can cast them but mind that this could lead to undefined behavior that mostly depends on machine, compiler and whatever. A better solution would have to have an `union` to have a set of predefined pointer functions. – Jack Jul 18 '12 at 15:56
  • At least with Intel C++ compiler 12 and Visual C++ 2010 64 x64 for Windows the function argument of the FunctionDecl struct cant be converted at compiling time from void* to " Class::*() – auroras Jul 18 '12 at 16:09
  • @auroras This approach won't work for class member functions anyway since pointers to member functions are often larger than a `void *`. Also, this solution is not at all scalable. Let's say all your functions take 3 arguments and return `void`. That's 8 `case` statements to cover every possible function call. Now, suppose you want to support 3 different return types as well ... – Praetorian Jul 18 '12 at 16:17
  • So what is the typical approach for this kind of issue? – auroras Jul 19 '12 at 15:07