I came up with something like this. It's not pretty, and it might not do what you want, but I think it's pretty close.
#include <iostream>
// Optionally, FunctionA
// could have some internal data that
// helps compute the results of a function call.
// For example, a table lookup functor may store
// a table.
struct FunctionA {
FunctionA() {}
// this one will take an int
void call( int n ) {
// example
std::cout << n * n << std::endl;
}
};
struct FunctionB {
FunctionB() {}
// this one will take 2 floats
void call( float a, float b ) {
std::cout << a * b << std::endl;
}
};
template <typename FunctionType>
class Test {
private:
// mutable because the function might want to
// modify its internal state when called.
mutable FunctionType fun_;
public:
void setFunction( const FunctionType &function ) {
fun_ = function;
}
FunctionType &action() { return fun_; }
};
int main() {
Test<FunctionA> testa;
testa.setFunction( FunctionA() );
testa.action().call( 3 );
Test<FunctionB> testb;
testb.setFunction( FunctionB() );
testb.action().call( 4.2, 3.6 );
}
The disadvantages I can think of are that you would have to wrap every function in a struct and wouldn't be able to change the function type associated with a class during runtime. You could maybe get around that by writing a copy constructor for Test and when you need to change the function, do something like:
Test<FunctionB> tester( tester ); // Where tester is Test<FunctionA>
I don't really know if that is even possible though. :p