Virtual Inheritance: Error: no unique final overrider

非 Y 不嫁゛ 提交于 2019-11-27 20:11:32

The most-derived class has to provide an implementation of the virtual functions in the virtual base class - otherwise how would it provide that base class interface, given the intermediate classes (i.e. your der1 and der2) provide two alternatives already - which one should it call? You have to disambiguate the situation (i.e. with der3::fun()).

Sure you're not actually calling der3::fun() as you're explicitly requesting base::fun(), but that doesn't mean the rules don't apply, any more than thinking you could instantiate an abstract class if you don't try to call the pure-virtual functions.... The program is ill-formed until the code ties off these loose ends.

Using the scope resolution operator to specify that you want to call base::fun doesn't make the error go away because the program would be ill-formed even with an empty main(). You simply are not allowed to have a situation in which a virtual function has more than one final overrider in any derived class that exists in your program.

Informally, just because trying to call p->fun() would be ambiguous, the program is ill-formed even if you don't do it.

Note: This is in contrast to the situation you have with overloaded functions, in which potential ambiguity is allowed---perhaps even unavoidable---as long as you avoid a call that actually would be ambiguous. Why are the rules different? Basically it is because even constructing an object of type der3 cannot be done in a sensible way---which version of fun should the vtable point to?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!