一个类可以重载公共继承接口中也存在的方法吗?看起来这是明确且有用的,但编译器(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);
}