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

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