c++ - 故意隐藏重载函数警告避免

标签 c++ clang warnings overloading hidden

考虑以下 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/

相关文章:

c++ - 使用第三方库的菱形继承(钻石问题)问题

c++ - Eigen 矩阵似乎保留了不同范围内的内存

macos - OS X 上的 Clang Address Sanitizer

c - 对于 C 开发人员,Clang 版本 2.8 和 3.1 之间有什么实际区别?

java - 泽西警告 - 无法解析为具体类型

python - 从 scikit-learn 中消除警告

c++ - 在 C++ 中实现 GLFW Windows 时“无法实例化抽象类”

c++ - 创建一个可以在类型和变量上调用的类型特征

c++ - Clang 在模板上下文中找不到函数定义后实例化的函数

Java:抑制警告 "X is marked unstable"