如何重写在多个继承中具有相同名称的基类的虚函数?

编程入门 行业动态 更新时间:2024-10-25 10:31:21
本文介绍了如何重写在多个继承中具有相同名称的基类的虚函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有两个基类B1和B2,以及一个从B1和B2派生的类D,如下所示:

Suppose I have two base classes B1 and B2, and a class D that derives from both B1 and B2 as follows:

class B1 { public: // ... virtual void foo() final { cout << "Hello, B1\n"; } }; class B2 { public: // ... virtual void foo() { cout << "Good riddance, B2!\n"; } }; class D :public B1, public B2 { // ... };

在设计类D时,我想从B2覆盖称为foo()的成员函数.但是,B1中的foo()被标记为final,并阻止我覆盖B2中的foo().从B2覆盖foo()的最佳方法是什么?

In designing the class D, I want to override the member function called foo() from B2; however, foo() in B1 is marked final and prevents me from overriding foo() in B2. What is the best approach to override foo() from B2?

推荐答案

我认为您无法按照问题中显示的方式来做.从N3337开始,§10.3/2 [class.virtual]

I don't think what you want to do is possible in the manner you've shown in the question. From N3337, §10.3/2 [class.virtual]

如果在类Base和类Derived中声明了虚拟成员函数vf,这些成员函数直接或间接从Base派生,则成员函数vf具有相同的名称,参数类型-clist(8.3.5),cv限定词和ref限定词(或没有ref限定词)声明为Base::vf,然后Derived::vf也是虚拟的(无论是否如此声明),并且它覆盖. ...

If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name, parameter-type-list (8.3.5), cv-qualification, and ref-qualifier (or absence of same) as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides Base::vf. ...

D::foo匹配B1::foo和B2::foo的所有那些条件,因此它将覆盖两者.并且由于B1::foo是final,所以代码格式错误.

D::foo matches all those criteria for B1::foo and B2::foo, hence it overrides both. And since B1::foo is final, the code is ill-formed.

一种解决方法是引入额外的继承级别.定义一个从B2派生并覆盖B2::foo的类,例如D2.然后D可以从B1和D2派生.

One workaround is to introduce an extra level of inheritance. Define a class, say D2, that derives from B2 and overrides B2::foo. Then D can derive from B1 and D2 instead.

class D2 : public B2{ public: virtual void foo() override { cout << __PRETTY_FUNCTION__ << '\n'; } }; class D :public B1, public D2 {}; D d; // d.foo(); // error - ambiguous D2& d2 = d; d2.foo(); // calls D2::foo B2& b2 = d; b2.foo(); // calls D2::foo B1& b1 = d; b1.foo(); // calls B1::foo

实时演示

更多推荐

如何重写在多个继承中具有相同名称的基类的虚函数?

本文发布于:2023-11-05 19:08:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561613.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   写在   函数   名称

发布评论

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

>www.elefans.com

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