4 ms·
Virtual inheritance means you have a diamond pattern and haven't separated your components properly. It should have never made it into the language as far as I'
by vvanders 10y ago
Virtual inheritance means you have a diamond pattern and haven't separated your components properly. It should have never made it into the language as far as I'm concerned.
- quotemstr 10y agoThat's a matter of taste; opinions differ. One perhaps less controversial case for virtual inheritance is in exceptions: see http://www.boost.org/doc/libs/1_53_0/libs/exception/doc/using_virtual_inheritance_in_exception_types.html http://www.boost.org/doc/libs/1_53_0/libs/exception/doc/usin...
- ioquatix 10y agoIt's useful for interfaces, e.g. virtual public IFoo, is it not?
- frozenport 10y agoInterfaces should have been compile time, but for reasons I don't know, they weren't implemented as such
- evanpw 10y agoWhat would that mean? No dynamic dispatch at all?
- frozenport 10y agoIt's possible to perform compile time de-virtualization. Half the time virtual is used to enforce inheritance rather than virtual dispatch.
- bubuga 10y ago> Virtual inheritance means you have a diamond pattern and haven't separated your components properly. I've seen this argument pop out time and again as if it was a mantra of sorts, and more often than not it comes from someone with a background almost exclusively founded on Java. Their line of reasoning essentially boils down to "Java doesn't support it, the people behind Java said something about it, therefore it's bad". But C++ isn't Java, nor does Java dictate what is correct or what makes sense. Particularly when the only assertion you could come up with to criticize multiple inheritance was a comment that had nothing to do with C++ or even OO programming, but only to do with your personal taste regarding your superficial impressions regarding software design.
- vvanders 10y agoOoo, that Java remark burns. I've only touched Java seriously in the last few years of my career, I'm a hardcore C++/native type. Here's the issues I take with virtual inheritance: 1. It complicates the vtable and function/member calls leading to another level of indirection, so from a performance perspective it's a negative mark. 2. The more fundamental problem is that you've botched the hasA vs isA association. If you have two classes that share a base class then that base class should be refactored into a component that can then be used as a member without polluting the class hierarchy. This leads to better composability since you can now pass this object around without pulling a whole class hierarchy with it. You're welcome to use virtual inheritance all you want in your projects but much like knowing what subset of C++ to use(and what not) you'll never see it in code I work on.
- quotemstr 10y ago> you've botched the hasA vs isA association No you haven't. In many senses, C++ inheritance is just shorthand for composition. That's why things like private inheritance are useful. Through this lens, virtual inheritance is automatic composition in which multiple composed objects each have a pointer to some shared object (the virtual base). It doesn't mess with hasA vs. isA at all. > knowing what subset of C++ to use This idea that a good coding standard necessarily bans parts of C++ has done massive damage to the C++ community. The correct subset of C++ to use is C++. Turning off parts of the language is just a cheap way to look sagacious while infantilizing developers. Every feature has its place.
- vvanders 10y agoI think we're just going to have to agree to disagree here. There's a been a fair number of projects I've worked on where exceptions and RTTI introduced too much memory/perf overhead so we explicitly didn't use them. I don't feel like we were worse off for not having them.
- quotemstr 10y ago