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
Language: Design Patterns
Expertise: Beginner
Feb 29, 2000

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Virtual Function

Question:
I'm surprised the following code doesn't compile:

class C1
{
public:
  virtual void f() { /* ... */ }
  void f(bool) { /* ... */ }
};

class C2 : public C1
{
public:
  void f() { /* ... */ }
};

main()
{
  C2 c2;
  c2.f(true);
}

I found that adding "using C1::f;" in C2 class solves the problem; however, I see no ambiguity in the call to f.

Answer:
Actually, your compiler is right. The C++ standard says that "a name can be hidden by an explicit declaration of that same name in a [...] derived class" (3.3.7/1). Thus, the f() defined in the derived class hides the one defined in the base class, although the two functions have different signatures. As you said, an explicit using-declaration in the derived class is needed to bring C1::f(bool)—as well as any other member named 'f'—into the scope of the derived class.

DevX Pro
 
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