18

In a derived class If I redefine/overload a function name from a Base class,
then those overloaded functions are not accessable/visible to derived class.
Why is this??

If we don't overload the oveloaded function from the base class in derived class then all the overloaded versions of that function are available to derived class
objects, why is this??
what is the reason behind this. If you explain this in compiler and linker level
that will be more helpful to me. is it not possible to support this kind of scinario??

Edited  
For examble:

class B  
{  

  public: 
     int f() {}
     int f(string s) {}
};

class D : public B
{
   public:
    int f(int) {}
};

int main()
{
   D d;
   d.f(1);
   //d.f(string);  //hidden for D
} 

Now object 'd' can't access f() and f(string).
esh
  • 273
  • 2
  • 6
  • 2
    Could you post some example code, your description is not clear at all. – Dmitry Yudakov Jul 08 '10 at 09:34
  • 1
    possible duplicate of [Why does an overridden function in the derived class hide other overloads of the base class?](http://stackoverflow.com/questions/1628768/why-does-an-overridden-function-in-the-derived-class-hide-other-overloads-of-the) – sbi Jul 08 '10 at 09:55
  • @sbi I'm sorry to post a duplicate question. Actually I tried for some time if similar question is there but I didn't find any. My searching pattern was wrong, thats y didn't find the existing question. – esh Jul 08 '10 at 10:08
  • No problem. This is what the feature to close duplicates is for. – sbi Jul 08 '10 at 10:22

2 Answers2

18

TTBOMK this doesn't have a real technical reason, it's just that Stroustrup, when creating the language, considered this to be the better default. (In this it's similar to the rule that rvalues do not implicitly bind to non-const references.)

You can easily work around it be explicitly bringing base class versions into the derived class' scope:

class base {
public:
  void f(int);
  void g(int);
};

class derived : public base {
public:
  using base::f;
  void f(float);
  void g(float); // hides base::g
};

or by calling the explicitly:

derived d;
d.base::g(42); // explicitly call base class version
sbi
  • 219,715
  • 46
  • 258
  • 445
  • 3
    This answer to another, related question provides the rationale - http://stackoverflow.com/questions/1628768/why-does-an-overridden-function-in-the-derived-class-hide-other-overloads-of-the/1629074#1629074 – jon hanson Jul 08 '10 at 09:46
  • 1
    I'm very happy to be member of this group. The members here are awesome. I'm so amused to see the replies within no time. – esh Jul 08 '10 at 09:50
  • @jon: Actually, this makes this question a duplicate. I voted to delete this one. – sbi Jul 08 '10 at 09:57
  • @jon. Thankyou for the link. Actually that question has got the full details I wanted to know. Here nobody answered to my 'why' part of the question!!! – esh Jul 08 '10 at 10:10
8

The functions are available, you just need to call them explicitly:

struct A {
   void f(){}
};

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

int main() {
   B b;
   b.f();     // call derived function
   b.A::f();  // call base function
}