c++ - Why does an overridden function in the derived class hide other overloads of the base class? -


consider code :

#include <stdio.h>  class base { public:      virtual void gogo(int a){         printf(" base :: gogo (int) \n");     };      virtual void gogo(int* a){         printf(" base :: gogo (int*) \n");     }; };  class derived : public base{ public:     virtual void gogo(int* a){         printf(" derived :: gogo (int*) \n");     }; };  int main(){     derived obj;     obj.gogo(7); } 

got error :

 >g++ -pedantic -os test.cpp -o test test.cpp: in function `int main()': test.cpp:31: error: no matching function call `derived::gogo(int)' test.cpp:21: note: candidates are: virtual void derived::gogo(int*)  test.cpp:33:2: warning: no newline @ end of file >exit code: 1 

here, derived class's function eclipsing functions of same name (not signature) in base class. somehow, behaviour of c++ not ok. not polymorphic.

judging wording of question (you used word "hide"), know going on here. phenomenon called "name hiding". reason, every time asks question why name hiding happens, people respond either called "name hiding" , explain how works (which know), or explain how override (which never asked about), nobody seems care address actual "why" question.

the decision, rationale behind name hiding, i.e. why designed c++, avoid counterintuitive, unforeseen , potentially dangerous behavior might take place if inherited set of overloaded functions allowed mix current set of overloads in given class. know in c++ overload resolution works choosing best function set of candidates. done matching types of arguments types of parameters. matching rules complicated @ times, , lead results might perceived illogical unprepared user. adding new functions set of existing ones might result in rather drastic shift in overload resolution results.

for example, let's base class b has member function foo takes parameter of type void *, , calls foo(null) resolved b::foo(void *). let's there's no name hiding , b::foo(void *) visible in many different classes descending b. however, let's in [indirect, remote] descendant d of class b function foo(int) defined. now, without name hiding d has both foo(void *) , foo(int) visible , participating in overload resolution. function calls foo(null) resolve to, if made through object of type d? resolve d::foo(int), since int better match integral 0 (i.e. null) pointer type. so, throughout hierarchy calls foo(null) resolve 1 function, while in d (and under) resolve another.

this behavior deemed undesirable when language designed. better approach, decided follow "name hiding" specification, meaning each class starts "clean sheet" respect each method name declares. in order override behavior, explicit action required user: redeclaration of inherited method(s) (currently deprecated), explicit use of using-declaration.

as correctly observed in original post (i'm referring "not polymorphic" remark), behavior might seen violation of is-a relationsip between classes. true, apparently decided in end name hiding prove lesser evil.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -