Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:13

0001 // name=cpp_virtual_hidden_warning_test ; gcc $name.cc -std=c++11 -lstdc++ -Wall -Woverloaded-virtual -DWITH_FIX -o /tmp/$name && /tmp/$name
0002 
0003 // https://stackoverflow.com/questions/15295317/xcode-why-is-a-warning-of-is-hidden-given-with-overloaded-virtual-functions
0004 
0005 
0006 #include <cstdio>
0007 
0008 class A {
0009 public:
0010   virtual void methodA(int) { printf("A::methodA(int)\n") ; }
0011   virtual void methodA(int, int, int) { printf("A::methodA(int, int, int)\n") ; }
0012 };
0013 
0014 class B : public A {
0015 public:
0016 
0017 #ifdef WITH_FIX
0018   using A::methodA;  //bring all overloads of methodA into B's scope 
0019 #endif
0020 
0021   virtual void methodA(int) { printf("B::methodA(int)\n") ; }
0022 };
0023 
0024 /**
0025 Problem with B is that it overrides only one of the methodA
0026 overloads which causes the other overload to be hidden.  
0027 
0028 **/
0029 
0030 
0031 
0032 
0033 int main()
0034 {
0035     A a;
0036     printf("a.methodA(7)\n"); 
0037     a.methodA(7);       //OK
0038     
0039     printf("a.methodA(7,7,7)\n"); 
0040     a.methodA(7, 7, 7); //OK
0041 
0042     B b;
0043     A *pa = &b;
0044 
0045     printf("pa->methodA(7) : calls B impl \n"); 
0046     pa->methodA(7);        //OK, calls B's implementation
0047 
0048     printf("pa->methodA(7,7,7) : calls A impl \n"); 
0049     pa->methodA(7, 7, 7);  //OK, calls A's implementation
0050 
0051 
0052     b.methodA(7); //OK
0053 
0054 #ifdef WITH_FIX
0055     b.methodA(7, 7, 7);  
0056 #else
0057     // without the fix : get compile error : methodA of B only accepts one int, not three.
0058 #endif
0059 
0060     return 0 ; 
0061 }