In the following code, calling operator()
with int
on the derived class does not compile, however, calling get()
with either int
or double
on the derived class works fine.
Both the operator()
with int
input and get()
are only implemented in the base class and not in the derived one.
Keeping or removing the virtual keyword does not affect the outcome.
class A {
public:
virtual double operator()(double& x) { return 0; }
virtual int operator()(int& i) { return 1; }
virtual double get(double& x) { return 2; }
virtual int get(int& i) { return 3; }
};
class B : public A {
public:
double operator()(double& x) { return 4; }
};
int main() {
B b;
A& a = *(&b);
double x = 100.0;
int i = 1;
std::cout << a(x) << "\n";
std::cout << b(x) << "\n";
std::cout << a(i) << "\n";
std::cout << "b(i)" << "\n"; // this line does not compile when it is b(i) instead of "b(i)"
std::cout << a.get(x) << "\n";
std::cout << b.get(x) << "\n";
std::cout << a.get(i) << "\n";
std::cout << b.get(i) << "\n";
}
Is there a fundamental difference between operator methods and other methods that causes this issue?