Using the overloaded base class function

I'm trying 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 various algorithms such as std :: accumulate or std :: transform. I am completely satisfied with the definition of the base class operator () (double, double). However, I cannot call it from a derived class. Do I have to rewrite the same code for each class that I get from the database?

+4
source share
2 answers

The problem 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 a derived class.

But there is a way to do this: you can simply use the using directive in a derived class:

 class derived: public base { using base::operator(); virtual double operator() (double val) { return someting_clever; } }; 
+6
source

Have you tried using the directive?

 class derived: public base { virtual double operator() (double val) { return someting_clever; } using base::operator(); }; 
+3
source

Source: https://habr.com/ru/post/1382729/


All Articles