0

Possible Duplicate:
C++: rationale behind hiding rule

Suppose I have a code:

class  A
{       
    public:
    void f(int s) {}
};



class B:public A
{      
    public:
    void f() {}
};

int main()
{      B ob;
   ob.f(4);
} 

Then in this case compiler generates an error that "no matching function for call to ‘B::f(int)'" But class B has inherited A as public so B must have the function "void f(int s)". Dont know why compiler is generating error here?

Community
  • 1
  • 1
Azazle
  • 37
  • 1
  • 9

3 Answers3

5

That is because B defines a different f, which hides the f inherited from A. If you want both available in B (which is likely), you must bring it into scope with a using-declaration:

class B : public A
{
  void f() {}
  using A::f;
};

This behaviour is specified in [class.member.loopkup], especially paragrah 4.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
3

When you declare void f() in B, this hides the void f(int) inherited from A. You can bring it back into scope with using:

class B: public A
{      
public:
    void f() {}
    using A::f;
};
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

This is called Hiding - you can check the C++ FAQ Entry. It describes the problem and the solution.

user93353
  • 13,733
  • 8
  • 60
  • 122