简体   繁体   English

隐藏重载的虚函数

[英]Hiding overloaded virtual function

Consider the following hierarchy of structs: 考虑以下结构层次结构:

struct I1 {
    virtual void doit() = 0;
};

struct I2 {
    virtual void doit(int) = 0;
};

struct I12 : I1, I2 {
    using I1::doit;
    using I2::doit;
};

struct Derived : I12 {
    void doit(int) override {}
};

Compiling this (using clang , or g++ with -Woverloaded-virtual ) gives me a warning: 编译它(使用clang ,或g++-Woverloaded-virtual )给我一个警告:

'Derived::doit' hides overloaded virtual function [-Woverloaded-virtual]

However, if I change I12 to the following, it compiles fine under clang , while g++ -Woverloaded-virtual still gives a warning: 但是,如果我将I12更改为以下内容,它会在clang下编译得很好,而g++ -Woverloaded-virtual仍然会发出警告:

struct I12 : I1, I2 {
    using I1::doit;
    void doit(int) override = 0;
};

Where is the difference between using I2::doit and void doit(int) override = 0 ? using I2::doitvoid doit(int) override = 0之间的区别在哪里? Naively, I would have thought that the former is sufficient to inform the compiler that I am aware that there are two versions of doit . 天真地,我会认为前者足以告知编译器我知道有两个版本的doit

It complains that doit is hidden in Derived . 它抱怨doit隐藏在Derived A fix: 修复:

struct Derived : I12 {
    using I12::doit; // Bring all doit declarations from I12 into this scope.
    void doit(int) override {}
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM