I designed a class with virtual function f() then within the derived class I rewrote it such as the following f(int) why can't I access the bottom class function toss the child instance ?

class B{
public:
    B(){cout<<"B const, ";}
    virtual void vf2(){cout<<"b.Vf2, ";}


};
class C:public B{
public:
    C(){cout<<"C const, ";}
    void vf2(int){cout<<"c.Vf2, ";}
};

int main()
{
    C c;
    c.vf2();//error should be vf2(2)

}

You need to do using B::vf2 to ensure that the function is recognized as throughout title research. Otherwise the moment the compiler finds a function title that suits while crossing the inheritance tree from child -> parent -> grand parent and so forth., the traversal stops.

class C:public B{
public:
    using B::vf2;
    C(){cout<<"C const, ";}
    void vf2(int){cout<<"c.Vf2, ";}
};

You're experiencing name hiding. Here's an explanation of why it takes place ?

In C++, a derived class hides any base class member of the identical title. You are able to still access the bottom class member by clearly being approved it though:

int main()
{
    C c;
    c.B::vf2();

}

You had been caught by title hiding.

Title hiding creeps up everywhere in C++:

int a = 0

int main(int argc, char* argv[]) {
  std::string a;
  for (int i = 0; i != argc; ++i) {
    a += argc[i];                    // okay, refers to std::string a; not int a;
    a += " ";
  }
}

And in addition it seems with Base and Derived classes.

The concept behind title hiding is robustness when confronted with changes. If the did not exist, during this situation, then consider an amount occur to:

class Base {
};

class Derived: public Base {
public:
  void foo(int i) {
    std::cout << i << "\n";
  }
};

int main() {
  Derived d;
  d.foo(1.0);
}

Basically would give a foo overload to Base which were a much better match (ie, going for a double directly):

void Base::foo(double i) {
  sleep(i);
}

Now, rather than printing 1, the program would sleep for 1 second!

This is crazy right ? This means that anytime you want to increase basics class, you have to look whatsoever the derived classes and make certain you do not accidentally steal some method calls from their store!!

To have the ability to extend basics class without destroying the derived classes, title hiding is necessary.

The using directive enables you to definitely import the techniques you undoubtedly need inside your derived class and also the relaxation are securely overlooked. This can be a whitened-listing approach.