KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
In C++, will a member function of a base class be overridden by its derived class function of the same name, even if its prototype (parameters' count, type and constness) is different ? I guess this a silly question, since many websites says that the function prototype should be the same for that to happen; but why doesn't the below code compile? It's a very simple case of inheritance, I believe. #include <iostream> using std::cout; using std::endl; class A {}; class B {}; class X { public: void spray(A&) { cout << "Class A" << endl; } }; class Y : public X { public: void spray(B&) { cout << "Class B" << endl; } }; int main() { A a; B b; Y y; y.spray(a); y.spray(b); return 0; } GCC throws error: no matching function for call to `Y::spray(A&)' note: candidates are: void Y::spray(B&)
Tags (comma-separated)
Save Edits
Cancel