I try to do something like
class base {
public:
virtual double operator() (double val) = 0;
virtual double operator() (double prev, double val) {
return prev + operator()(val);
}
};
class derived: public base {
virtual double operator() (double val) {
return someting_clever;
}
};
I want to overload the operator() to use it with different algorithms such as std::accumulate or std::transform. I'm totally happy with the base class definition of operator()(double, double). However I can't call it from the derived class. Do I have to rewrite the same code for each class I derive from base?
The problem here is that defining a function in a derived class hides the same function in the base class; this is the reason why you cannot access operator()(double, double)
from the derived class.
But there is a way to do it: you can just use a using
directive in your derived class:
class derived: public base {
using base::operator();
virtual double operator() (double val) {
return someting_clever;
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With