Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Home » Tip Bank » C++ » Coding
Language: C++
Expertise: All
Jun 4, 1998

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 {
FILE *pf;
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.
			//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


Danny Kalev
Thanks for your registration, follow us on our social networks to keep up-to-date