devxlogo

Hiding a base class member function

Hiding a base class member function

A derived class may hide a member function declared in its base class by using the same function name, but with a different signature, or list of arguments. Usually, this case suggests a programmer’s mistake, especially if the hidden function is virtual. However, it can also be used as a means of hiding a base class member function when the designer of the derived class decides that any invocation of the original member function is undesirable or even dangerous:

 class B {private:FILE *pf;public: //...virtual void close(FILE *f) //may throw an exception};class D : public B { //no file I/O in D, calling B::close from                  //here is dangerous and never needed.public: 			//a 'neutralized' close, harmless    void close(int i){} //hiding B::close(), not overriding it};void f() {B b;FILE *p;//....D d;d.close(p);//compile-time error: B::close()not accessible}

devx-admin

Share the Post: