devxlogo

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 Dpublic:  void func(int n); // D now has two overloaded versions of func()};D d;d.func ( ); // OKd.func ( 10 ); // OK

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Seven Service Boundary Mistakes That Create Technical Debt

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.