【问题标题】:Overloads of inherited member functions继承的成员函数的重载
【发布时间】:2010-10-03 01:56:46
【问题描述】:

一个类能否重载也存在于公共继承接口中的方法? 看起来这是明确且有用的,但编译器(VC、Intel、GCC)都抱怨,至少在我的构造中是这样。 下面是一个玩具示例。继承的反弹() 函数有两个明确的重载,但这不会编译。如果你重命名任一类中的rebound() 方法,它可以正常工作,但如果它们共享相同的成员函数名称(即使它们被不同的参数类型重载!)你会得到一个致命错误“函数调用的参数太少。”

解决方法很简单(我只是重命名方法),但我只是想了解这是否是 C++ 限制(以及为什么会这样)。


#include 
class Bound {
public:
  Bound() : x0(0.0), x1(0.0) {};
  Bound(double x) : x0(x), x1(x) {};
  double width() const {return x1-x0;}
  void rebound(const Bound *a, const Bound *b);
private:
  double x0, x1;
};

void Bound::rebound(const Bound *a, const Bound *b)
{
  if (a && b) {
    x0=std::min(a->x0, b->x0);
    x1=std::max(a->x1, b->x1);
  }
}

class Node : public Bound {
public:
  Node(double x) : Bound(x), left(0), right(0) {};
  Node(Node *a, Node *b) : left(a), right(b) {rebound();}
  void rebound() { rebound(left, right); }
private:
  Node *left;
  Node *right;
};


int main() {
  Node A(1.0);
  Node B(2.0);
  Node C(&A, &B);
}

【问题讨论】:

    标签: c++ inheritance overloading


    【解决方案1】:

    你可以做三件事:

    1。取消隐藏基类方法

    Node 声明中添加using

    using Bound::rebound;
    void rebound() { rebound(left, right); }
    

    2。显式引用基类方法

    使用绑定命名空间:

    void rebound() { Bound::rebound(left, right); }
    

    3。定义/重新定义派生类中的所有重载

    将实现委托给基类(如果这是在标头中完成的,由于内联,不应该有任何惩罚):

    void rebound(const Bound *a, const Bound *b) { Bound::rebound(a, b); };
    void rebound() { rebound(left, right); }
    

    更多信息: https://isocpp.org/wiki/faq/strange-inheritance#overload-derived

    【讨论】:

    • Scott Meyers 还在其“Effective C++(第三版)”第 6 章中广泛讨论了继承的陷阱。
    • #1 太棒了,你推荐哪一个?
    【解决方案2】:

    当你在子类中声明一个同名但签名不同的方法时,它实际上对父类隐藏了版本。

    您可以将其专门称为 Bound::rebound(...) 或使用 using 关键字。

    here

    【讨论】:

      【解决方案3】:

      这被称为隐藏父成员函数。您可以显式调用它(@Ates Goral 所说的Bound::rebound(left, right)),也可以在Node 类定义中添加using Bound::rebound

      请参阅http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.9 了解更多信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-11
        • 1970-01-01
        • 1970-01-01
        • 2017-01-05
        • 1970-01-01
        • 2020-08-12
        • 2013-05-09
        相关资源
        最近更新 更多