dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
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
Home » Tip Bank » C++
Language: C++
Expertise: Intermediate
Oct 15, 2001

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Overloading and Overriding


Overriding a method suppresses the visibility of any overloads in the base class for the overridden method. Say you have a base class CBase where there are two trivial overloads:
 
class CBase {
 CBase ();
 Func (int param);
 Func (float param);
};

In the derived class, you overload Func:
 
class CDerived {
 CDerived ();
 Func (float param);
};

Say you do something like this:
 
CDerived derived;
int param = 1;
derived.Func (param);

You expect that CBase::Func (int) should be called as you have passed a parameter of type int and this method would be the best match. You'd be surprised.

If you run it in debug step by the step, you'll see that instead CDerived::Func (float) is called. Why? Because when you override a method, all the overloads in the base class for that method become invisible. What happens is that a conversion from int to float is made for param and then CDerived::Func (float) is called. Perhaps not exactly what you wanted to do.

Here's a workaround:

In CDerived, bring the overloads in the CDerived interface by using the C++ statement "using".
 
class CDerived {
 CDerived ();
 using CBase::Func;
 Func (float param);
};

Now everything's fine and the compiler will call CBase::Func (int) as you'd probably expected.
Ciprian Miclaus
 
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date