Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
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 {
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

}

Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap