Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using overloaded function of the base class

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?

like image 928
Michael Avatar asked Jan 19 '23 05:01

Michael


1 Answers

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;
    }
};
like image 55
Gorpik Avatar answered Jan 29 '23 07:01

Gorpik