具有相同名称的虚拟成员函数的继承

编程入门 行业动态 更新时间:2024-10-25 06:25:14
本文介绍了具有相同名称的虚拟成员函数的继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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.

更多推荐

具有相同名称的虚拟成员函数的继承

本文发布于:2023-11-05 19:09:38,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   成员   名称

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!