I'm still playing with this, primarily out of curiousity. I think that my main function's vector of function pointers serves as a "registry" -- I don't see why anything more elaborate is needed. The question is just how it gets loaded.
Implementing Salem's suggestion is straightforward enough:
Code:
// fn_pointer2.cpp
#include <iostream>
#include <vector>
#include "funcs2.h"
#include <cassert>
using namespace std;
int main () {
vector<int (*)(int,int)> funcs;
functions::regis(funcs);
assert(!funcs.empty());
int x,y,op,sum;
cout << "1. add\n2. multiply\n\tChoose 1 or 2\n";
cin >> op;
cout << "enter 2 operands:\n";
cin >> x >> y;
sum = funcs[op-1](x,y);
cout << "the result is " << sum << "\n";
}
Code:
// funcs2.h
#ifndef funcs2_h_
#define funcs2_h_
#include <vector>
struct functions {
static int add(int,int);
static int mult(int,int);
static void regis( std::vector<int (*)(int,int)>& );
};
#endif
Code:
// funcs2.cpp
#include "funcs2.h"
// user-provided registration function:
// user can implement this either by adding a line for each function
// or by iterating through an array containing a pointer to each function
void functions::regis(std::vector<int (*)(int,int)> ®istry) {
registry.push_back(&add);
registry.push_back(&mult);
}
int functions::add(int x, int y) {return x + y;}
int functions::mult(int x, int y) {return x * y;}
Then I thought I would see how this would be done in a class or struct. So I wrapped the functions in a struct (called "functions") & gave it a constructor that takes a vector of function pointers as an argument. Since now these are member functions, I had to change the type of the vector to <int (functions::*)(int,int)>. But now the compiler is complaining about the way the main function uses the pointers to call the functions & I can't figure out what it wants me to do.
Here's the error:
Code:
g++ fn_pointer3.cpp funcs3.cpp
fn_pointer3.cpp: In function `int main()':
fn_pointer3.cpp:20: error: must use .* or ->* to call pointer-to-member
function in `(&funcs)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = int
(functions::*)(int, int), _Alloc = std::allocator<int (functions::*)(int,
int)>]((op - 1)) (...)'
and here's the code:
Code:
// fn_pointer3.cpp
#include <iostream>
#include <vector>
#include "funcs3.h"
#include <cassert>
using namespace std;
int main () {
// vector<int (*)(int,int)> funcs;
vector<int (functions::*)(int,int)> funcs;
functions f(funcs);
assert(!funcs.empty());
int x,y,op,sum;
cout << "1. add\n2. multiply\n\tChoose 1 or 2\n";
cin >> op;
cout << "enter 2 operands:\n";
cin >> x >> y;
sum = funcs[op-1](x,y);
cout << "the result is " << sum << "\n";
}
Code:
// funcs3.h
#ifndef funcs3_h_
#define funcs3_h_
#include <vector>
struct functions {
functions( std::vector<int (functions::*)(int,int)> );
int add(int,int);
int mult(int,int);
};
#endif
Code:
// funcs3.cpp
#include "funcs3.h"
int functions::add(int x, int y) {return x + y;}
int functions::mult(int x, int y) {return x * y;}
functions::functions(std::vector<int (functions::*)(int,int)> registry) {
registry.push_back(&functions::add);
registry.push_back(&functions::mult);
}