When compiling the following code :
class Base {
public:
Base(){}
virtual ~Base(){}
virtual bool equals(const Base* rhs) const { return false;}
};
class DerivedA : public Base {
public:
DerivedA() : Base(), val(0) {}
virtual ~DerivedA() {}
virtual bool equals(const DerivedA* rhs) const { return this->val == rhs->val;}
int val;
};
class DerivedB : public Base {
public:
DerivedB() : Base(), val(0) {}
virtual ~DerivedB() {}
virtual bool equals(const DerivedB* rhs) const { return this->val == rhs->val;}
int val;
};
int main() {
const DerivedA a;
const DerivedB b;
std::cout << a.equals(&b);
}
I get:
../main.cpp:104:26: error: no matching function for call to ‘DerivedA::equals(const DerivedB*) const’
std::cout << a.equals(&b);
^
../main.cpp:104:26: note: candidate is:
../main.cpp:88:15: note: virtual bool DerivedA::equals(const DerivedA*) const
virtual bool equals(const DerivedA* rhs) const { return this->val == rhs->val;}
^
../main.cpp:88:15: note: no known conversion for argument 1 from ‘const DerivedB*’ to ‘const DerivedA*’
But why doesn't it use the base class virtual bool equals(const Base* rhs) const
?