本文介绍了具有相同名称的虚拟成员函数的继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class A { A(){}; virtual〜A(){}; virtual void Start(){}; virtual void开始(float a){}; }; class B:public A {}; class C:public A { virtual void Start(float a){}; } ... B BObj; BObj.Start(); // - >罚款,没有抱怨从g ++ ... ... C CObj; CObj.Start(); // - >不好 - >错误:没有匹配函数调用'C :: Start()' ...
我怀疑问题来自两个虚函数具有相同的名称,但具有不同的参数签名。我想知道的是,这是一个特定于g ++的错误消息,它是如何实现vtable的,还是基于C ++标准的错误。
解决方案重载函数隐藏所有其他开始函数。要使用它们,请使用A :: Start 添加:
class C:公共A { public:使用A :: Start; virtual void开始(float a){}; }同时使开始也可以在A中公开。
编辑
class A { A() {}; virtual ~A() {}; virtual void Start() {}; virtual void Start(float a) {}; }; class B : public A { }; class C : public A { virtual void Start(float a) {}; } ... B BObj; BObj.Start(); // -> fine, no complain from g++ ... ... C CObj; CObj.Start(); // -> not fine -> error: no matching function for call to ‘C::Start()’ ...I suspect that the problem comes from that both virtual functions have the same name, but different parameter signature. What I would like to know is that this is a g++-specific error message, how it is implemented the vtable, or it is an error based on the C++ standard.
解决方案Overloading function hides all other Start functions. To use them add using A::Start:
class C : public A { public: using A::Start; virtual void Start(float a) {}; }Also make Start public in A too.
Edit: Here you can find why derived class hides base class functions.
更多推荐
具有相同名称的虚拟成员函数的继承
发布评论