If the base class has an overloaded set of member functions and the derived class overrides some but not all of that set, the redefined member functions will hide the other overloads. The work-around is to use the using syntax. The following example shows class Base with two overloaded member functions called f. #include <iostream> using namespace std; class Base { public: virtual ~Base() throw(); virtual void f(int x) throw(); virtual void f(float x) throw(); }; Base::~Base() throw() { } void Base::f(int x) throw() { cout << "Base::f(int)\n"; } void Base::f(float x) throw() { cout << "Base::f(float)\n"; } Now suppose the author of class Derived wants to override one of the two f() member functions. In this case the derived class should also say using Base::f; to avoid confusing users: class Derived : public Base { public: virtual void f(int x) throw(); using Base::f; <-- 1 }; void Derived::f(int x) throw() { cout << "Derived::f(int)\n"; }
Because of the using Base::f; line in the derived class, f(float) is not hidden: void sample(Base& b, Derived& d) { b.f(42); d.f(42); b.f(3.14f); d.f(3.14f); <-- 1 } int main() { Derived d; sample(d, d); }
The output of this program demonstrates that the behavior depends on the type of the object, not the type of the reference: Derived::f(int) Derived::f(int) Base::f(float) Base::f(float) This guideline applies only to public inheritance; hiding base class member functions is fine for private or protected inheritance (see FAQ 37.01). |