遇到这个问题,后来找到https://stackoverflow.com/questions/18515183/c-overloaded-virtual-function-warning-by-clang。
下面这段代码
struct Base
{
virtual void * get(char* e);
};
struct Derived: public Base {
virtual void * get(char* e, int index);
};
会爆出一个警告:
warning: 'Derived::get' hides overloaded virtual function [-Woverloaded-virtual]
这个警告是为了防止误写:
struct chart; // let's 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
};
一个解决办法是:
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);
};
根据需要,可以把 Base 的函数直接隐藏掉:
struct Derived: public Base
{
virtual void * get(char* e, int index);
private:
using Base::get;
};
Q. E. D.