I am having below code,
#include <iostream>
#include <string>
class Base {
public:
void operator()(std::string str) {
std::cout << "base str: " << str << std::endl;
}
virtual void operator()(double d) {
std::cout << "base double: "<< d << std::endl;
}
};
class Derived: public Base {
public:
void operator()(double d) override {
std::cout << "derived double: "<< d << std::endl;
}
};
int main() {
Derived derived;
derived(8.0);
derived(std::string("hi"));
}
The above code wont compile as it will complain that no match for call to ‘(Derived) (std::string)’
, but if I change derived class into below,
class Derived: public Base {
public:
using Base::operator();
void operator()(double d) override {
std::cout << "derived double: "<< d << std::endl;
}
};
The code will compile and works fine, I wonder why is this the case, as I am under the impression that all the Base
class public function will be in Derived
?