Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: C++
Expertise: Intermediate
Oct 16, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Overloading a Member Function Across Class Boundaries

Since a class is a namespace, the scope for overloading a member function is confined to the class containing this function. Sometimes, it is necessary to overload the same function in its class as well as in a class derived from it. Using an identical name in a derived class merely hides the base class' function, rather than overloading it:
class B {
public: void func(); 
class D : public B { 
public:  void func(int n); //now hiding B::f, not overloading it  
D d;
d.func();//compilation error. B::f is invisible in d; 
d.func(1); //OK, D::func takes an argument of type int
In order to overload (rather than hide) a function of a base class, you must inject it explicitly into the namespace of the derived class like this:
class D : public B { 
using B::func; // inject the name of a base member function into the scope of D
public:  void func(int n); // D now has two overloaded versions of func()
D d;
d.func ( ); // OK
d.func ( 10 ); // OK
Danny Kalev
Comment and Contribute






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



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