c++ - Undo name hiding by "using" keyword. Does not work in grandchild class -
for example in below program undo name hiding "using" keyword. if have base , 1 derived class "im getting expected ambiguous call error". if have 2 derived class(child , grand child) child , grand child having same overloaded function here undo name hiding "using" keyword. getting compiled , got output. question why im not getting error "ambiguous call overloaded function".
class basenamehiding { protected: int namehidingexample(int t) { cout<<"im baseeeeeeeeeeee"<<endl; return 0; } }; class derivednamehiding:public basenamehiding { public: float namehidingexample(float s) { cout<<"im derived"<<endl; return 0; } using basenamehiding::namehidingexample; }; class grandderivednamehiding:public derivednamehiding { public: float namehidingexample(float f) { cout<<"im grand derived"<<endl; return 0; } using basenamehiding::namehidingexample; using derivednamehiding::namehidingexample; }; int main() { char a;float f = 0.0; derivednamehiding derived; derived.namehidingexample(0); grandderivednamehiding grandchild; grandchild.namehidingexample(f); cin>>a; } //output im baseeeeeeeeeeee im grand derived
you have encountered special rule of using-declarations. c++14 [namespace.udecl]/15:
when using-declaration brings names base class derived class scope, member functions , member function templates in derived class override and/or hide member functions , member function templates same name, parameter-type-list, cv-qualification, , ref-qualifier (if any) in base class (rather conflicting). [...] [ example:
struct b { virtual void f(int); virtual void f(char); void g(int); void h(int); }; struct d : b { using b::f; void f(int); // ok: d::f(int) overrides b::f(int); using b::g; void g(char); // ok using b::h; void h(int); // ok: d::h(int) hides b::h(int) }; void k(d* p) { p->f(1); // calls d::f(int) p->f(’a’); // calls b::f(char) p->g(1); // calls b::g(int) p->g(’a’); // calls d::g(char) }
— end example ]
Comments
Post a Comment