Passing a function pointer by reference
<p>Hi I am trying to learn some function pointers in C/C++ and I was trying to write the following C++ code with gcc on Ubuntu. </p>

<p>This code should execute the multiply or or the add function depending on the 
preprocessor flag -DADD or -DMULTIPLY provided during compilation</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;iomanip&gt;
//Adds two numbers
int add(int a, int b)
{
  return a+b;
}
//Multiplies two numbers
int multiply(int a, int b)
{
  return a*b;
}

//Function to set the correct function to be executed.
//All functions here should have the same signature.
void functionsetter( void (*ptr2fun)(int,int)  )
{
#ifdef ADD
  ptr2fun   = add;
#endif

#ifdef MULTIPLY 
   ptr2fun  = multiply
#endif
} 

int main(int argc, char *argv[])
{
  int a = 5;
  int b = 6;

  void (*foo)(int,int);
  functionsetter(foo);

   return 0;
}
</code></pre>

<p>I cannot figure out how to pass the function pointer <code>foo</code> to the <code>function-setter</code> function by reference. Can someone help me out on this.I am sure the declaration of </p>

<p><code>functionsetter</code> is wrong in the code, Please let me know how to fix it.</p>

<p>I am trying to compile this with <code>g++ -O2 -g -Wall  -DADD main.cpp -o main</code></p>

<p>Note: I want to use such preprocessor flags and function pointers in some other-code elsewhere. 
Please let me know if such a thing is a good idea / practice.</p>
