考虑以下 C++ 示例 main.cpp
文件:
class FooIf
{
public:
virtual int handle(char *req, char *res) = 0;
};
class BarIf
{
public:
virtual void handle(char *msg) = 0;
};
class Bar : private BarIf
{
private:
void handle(char * msg){}
};
class Zoo : public FooIf, public Bar
{
public:
using FooIf::handle;
public:
int handle(char *req, char *res){ return (0); }
};
int main(){
Zoo zoo;
return (0);
}
我收到这个警告:
$ clang++ -ggdb -c main.cpp -Wall
main.cpp:23:6: warning: 'Zoo::handle' hides overloaded virtual function [-Woverloaded-virtual]
int handle(char *req, char *res){ return (0); }
^
main.cpp:17:7: note: hidden overloaded virtual function 'Bar::handle' declared here: different number of parameters (1 vs 2)
void handle(char * msg){}
^
现在 .. 我确实在隐藏 Bar::handle
,而且我是有意的。
有没有办法在解决这个问题时避免抑制警告?
不必说 g++
对此一点也不提示。
最佳答案
您应该考虑不同的设计。如果您真的需要它,这似乎是一个代码味道/设计问题。
如果(无论出于何种原因)您真的想这样做,您可以在特定位置关闭警告。有关信息,请参阅此处 http://clang.llvm.org/docs/UsersManual.html#controlling-diagnostics-via-pragmas
在你的情况下,这看起来像这样
class Zoo : public FooIf, public Bar
{
public:
using FooIf::handle;
public:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Woverloaded-virtual"
int handle(char *req, char *res){ return (0); }
#pragma clang diagnostic pop
};
关于c++ - 故意隐藏重载函数警告避免,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39772139/