+ 1
Overloading for derived class
class A { public: void foo() {} void foo(int x) {} //Why this function is not overloaded for a class B? }; class B: public A { public: void foo() {} }; int main() { B obj; obj.foo(1); //Compiler error return 0; }
1 Odpowiedź
+ 3
If you place a same-named function with a different signature (no int) in the derived class, all same-named functions of the base class get lost. If you still want to have the base class function available in B you will have to import with a using declaration just before the new version of f
using B::foo;
If you want to use polymorphism too, don't forget to put the virtual-keyword in the base class declaration of the function:
virtual void foo(int x);