C ++ clang virtual function warning overloaded?

clang issues a warning when compiling the following code:

struct Base { virtual void * get(char* e); // virtual void * get(char* e, int index); }; struct Derived: public Base { virtual void * get(char* e, int index); }; 

Warning:

 warning: 'Derived::get' hides overloaded virtual function [-Woverloaded-virtual] 

(the warning should be included, of course).

I do not understand why. Note that uncommenting the same declaration in Base disables the warning. I understand that since the two get () functions have different signatures, there can be no hiding.

Is it correct? Why?

Please note that this is on MacOS X running the latest version of Xcode.

 clang --version Apple LLVM version 5.0 (clang-500.1.74) (based on LLVM 3.3svn) 

Update: Same behavior with Xcode 4.6.3.

+44
c ++ overloading virtual warnings hidden
Aug 29 '13 at 15:32
source share
4 answers

This warning is intended to prevent accidental overload hiding when planning overrides. Consider a slightly different example:

 struct chart; // let pretend this exists struct Base { virtual void* get(char* e); }; struct Derived: public Base { virtual void* get(chart* e); // typo, we wanted to override the same function }; 

As this warning, this does not necessarily mean that it is a mistake, but it can mean one thing. Typically, such warnings have the ability to disable them, being more explicit and letting the compiler know that you intended to write what you wrote. I believe that in this case you can do the following:

 struct Derived: public Base { using Base::get; // tell the compiler we want both the get from Base and ours virtual void * get(char* e, int index); }; 
+75
Aug 29 '13 at 15:38
source share

R. Martinho Fernandes's solution works great if you really want the get() method to accept a single char * argument in the Derived scope.

Actually, the provided fragment does not need virtual methods (since Base and Derived do not use any method with the same signature).

Assuming that there is actually a need for polymorphism, the behavior of concealment can, however, be what is supposed to be. In this case, you can locally disable the Clang warning with the following pragma:

 #pragma clang diagnostic push #pragma clang diagnostic ignored "-Woverloaded-virtual" // Member declaration raising the warning. #pragma clang diagnostic pop 
+16
Nov 22 '13 at 13:57
source share

A warning means that there will be no void * get (char * e) function in the Derived class, because it is hidden by another method with the same name. The compiler will not look for a function in the base classes if the derived class has at least one method with the specified name, even if it has other arguments.

This sample code will not compile:

 class A { public: virtual void Foo() {} }; class B : public A { public: virtual void Foo(int a) {} }; int main() { B b; b.Foo(); return 0; } 
+10
Aug 29 '13 at 15:47
source share

Another way to turn off a warning that preserves the public structure's public interface is as follows:

 struct Derived: public Base { virtual void * get(char* e, int index); private: using Base::get; }; 

This prevents the user Derived calling Derived::get(char* e) when the warning is turned off:

 Derived der; der.get("", 0); //Allowed der.get(""); //Compilation error 
+10
May 12 '15 at 1:45
source share



All Articles