#include #include using namespace std; // http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.10 class Func { public: static string __doc__; virtual double operator()(double x, double y) = 0; virtual ~Func() = 0; }; inline Func::~Func() {} class FuncAdd: public Func { public: virtual double operator()(double x, double y) { return x+y; } }; FuncAdd::__doc__ = "add(x,y)\nReturns the sum of x and y.\n"; class FuncSub: public Func { public: virtual double operator()(double x, double y) { return x-y; } }; FuncSub::__doc__ = "sub(x,y)\nReturns the difference of x-y.\n"; class FuncMul: public Func { public: virtual double operator()(double x, double y) { return x*y; } }; FuncMul::__doc__ = "mul(x,y)\nReturns the product of x and y.\n"; typedef Func* func_ptr; int main() { double x,y; const int N = 3; func_ptr f[N]; f[0] = new FuncAdd(); f[1] = new FuncSub(); f[2] = new FuncMul(); x = 10; y = 2; cout << "x = " << x << ", y = " << y << endl << endl; for(int i=0; i < N; i++) { cout << f[i]->__doc__; cout << (*f[i])(x,y) << endl << endl; } return 0; }