Please forgive me if I fundamentally misunderstand something about dispatch in C++!
The code as pasted below works as intended.
However, if I uncomment/add the additional operator<< method in Derived (which should handle a different agrument type), C++ is then unable to resolve the previously-working dispatch "*this << something" (which, in the absence of that method, is successfully dispatched to Base as intended).
It says:
main.cpp:25:15: Invalid operands to binary expression ('Derived' and 'Something')
Please could someone explain to me why this happens?
Thanks in advance
(Mac OS Monterey 12.2.1, xCode 13.2.1)
#include <iostream>
class Something { };
class SomethingUnrelated { };
class Base {
public:
virtual void method() = 0;
void operator<<(Something something) {
method();
}
};
class Derived : public Base {
public:
void method() { std::cout << "Derived::method() - Yay!" << std::endl; }
void work(Something something) {
*this << something;
}
// void operator<<(SomethingUnrelated somethingUnrelated) { }
};
int main(int argc, const char * argv[]) {
Something something;
Derived derived;
derived.work(something);
return 0;
}