I would like to write an abstract base class
class func_double_double_t : public unary_function<double, double>
{
virtual double operator()(double x) = 0;
};
and specialize it for several different types
class func_pow_t : public func_double_double_t
{
public:
func_pow_t(double exponent) : exponent_(exponent) {};
virtual double operator()(double x) { return pow(x, exponent_); };
}
class func_exp_t : public func_double_double_t
...
and pass these to a function when necessary:
double make_some_calculation(double num, func_double_double_t f)
{
return f(x);
}
But I can’t define an object of type func_double_double_t because it’s abstract. I can pass a pointer to the function, but using f like f->operator()(num) seems against the spirit of operator overloading in the first place. ((*f)(num) is better, but still.)
Is there way to make operator overloading and such abstraction play nicely together?
You can simply pass a reference into your function: