Inheritance by dominance - is it really bad?

蓝咒 提交于 2019-12-03 09:58:40

When performing virtual inheritance, it is a bad idea to not explicitly override every member in the most derived class. Else, you are asking for your code to die a horrible death when someone changes one of your base classes that inherits from the virtual base. There's nothing actively wrong with this, your program won't crash or anysuch, but it's a maintainability bad idea. If you want to call the Bar::foo version, then you should just delegate to it in Quux::foo.

As far as the runability of your code is concerned, it is just there to remind you that Bar is the dominant implementation of foo. It is just there to inform you, it's not really a warning, so that if you're debugging and think it's Baz you don't pull your hair out :).

Is there a reason you aren't writing:

class Quux : public Bar, public Baz
{
    using Bar::foo;
};

?

This gives you the same level of reuse, without the fragility.

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