• Hasseman (unregistered)

    I'm virtually the first!

  • Giulio (unregistered)

    And now all your inheritance is in a vtable, and requires indirection.

  • (nodebb) in reply to Giulio

    It's actually worse than that, because the construction of a virtual-inheritance object is ... fraught. If the base class is totally non-concrete, you can sort of get away with it, except that in that case the virtual inheritance has no purpose.

    If, on the other hand, the base class has stuff in it, then it's necessary to have ways for methods of the derived class to find the base object because it might not be adjacent to the derived part of the combination, and that imposes a performance penalty (even though it might be small). It gets worse as you put in more layers of chained virtual-virtual-virtual inheritance.

  • (nodebb)

    I might be in the minority but I'm ok with multiple inheritance. C++ may not be perfect but not having it is a pain. Languages do things like mix ins and C#'s new feature of default implementation of an interface method. It's basically like saying, we're ok with MI but no virtual method.

  • my name is missing (unregistered)

    With C you can shoot yourself in the foot, but C++ comes with a mini-gun capable of obliterating your anatomy like an A10 Warthog.

  • (nodebb)

    The first OO language I used a significant amount was Common Lisp with CLOS, and before that Lisp Machine Lisp with Flavors. They have multiple inheritance, and all inheritance is virtual (I suppose you could define a new metaclass that implements a different inheritance scheme, so I'm just talking about the standard behavior). No one considered it much of a problem. At worst it complicates the algorithm for determining the total order of the class hierarchy based on the partial orders of each inheritance step, although in practice you rarely depend on the specific order.

  • snoofle (unregistered) in reply to my name is missing

    +10!!! I once inherited (no pun intended) a system that had an inheritance hierarchy more than 30 classes deep. Basically, each subclass was a method in a straight-line call chain. Yeech.

  • (nodebb)

    You have to be an old-timer like me to know what an El Camino is without looking it up. Hey! Get off my lawn!

  • Tim! (unregistered) in reply to Mr. TA

    Interface inheritance is pretty fundamentally different from class inheritance; e.g. the diamond problem does not exist during interface inheritance. Mixins are an orthogonal concept. It makes the most sense to allow classes to inherit from zero or one classes, to implement zero to many interfaces, and to include zero to many mixins.

    The problem with C++ is that these separate concepts (and others) are all treated as the same kind of nail by its big inheritance hammer. In C++ interfaces and mixins only exist by local convention, so it is difficult if not impossible to enforce the constraints that these concepts require. Foot shooting ensues.

    I recommend implementing mixins with a component model rather than with inheritance.

  • (nodebb) in reply to Tim!

    There's no such thing as interface inheritance. Interface implementation is different than class inheritance sure, but there are many examples where multiple inheritance makes conceptual sense. Regarding mix ins, that's what I've basically been doing, however awkwardly the resulting code ends up looking.

  • Mr Flibble (unregistered) in reply to Mr. TA

    I'm not sure about C or C++ but interface inheritance does exist. C# for example allows interfaces to be inherited

  • (nodebb) in reply to Mr Flibble

    Correct, I misspoke on that

  • StrongBad Endless Frontier (unregistered)

    "Where does ElCamino get its drive() function from?"

    The compiler forces you to disambiguate and everyone gets on with their lives?

    Oh wait no, the Gang of Four don't like it, so we all have to pretend it's way harder than that and hobble every modern language.

  • xtal256 (unregistered) in reply to bradwood

    Or you're a fan of The Black Keys.

  • Zug (unregistered)

    Back in the day I worked for a company with everything written in C++ where they had various "interfaces" as base classes and implementation as base classes and multiple inheritance for the actual classes used (as advocated by Stroustrup at the time). The system was large and mature and had lots of customers and deployments. Of course I got to the point where I needed to inherit from two of these actual classes to do what I needed to do. All hell broke loose and nothing worked. I found out what was going wrong and "discovered" the diamond problem for myself and realized that I could solve my problem easily: all I had to do was go back through (nearly) every class in the library and mark the inheritance as virtual and re-build everything and re-release everything. Which never happened, of course. At the time I wished that someone, back in time, had religiously marked all the inheritance as virtual so that my problem would have been easily solved. (Event though 90% of the code had been written before virtual inheritance existed in C++.)

  • Zug (unregistered)

    Back in the day I worked for a company with everything written in C++ where they had various "interfaces" as base classes and implementation as base classes and multiple inheritance for the actual classes used (as advocated by Stroustrup at the time). The system was large and mature and had lots of customers and deployments. Of course I got to the point where I needed to inherit from two of these actual classes to do what I needed to do. All hell broke loose and nothing worked. I found out what was going wrong and "discovered" the diamond problem for myself and realized that I could solve my problem easily: all I had to do was go back through (nearly) every class in the library and mark the inheritance as virtual and re-build everything and re-release everything. Which never happened, of course. At the time I wished that someone, back in time, had religiously marked all the inheritance as virtual so that my problem would have been easily solved. (Event though 90% of the code had been written before virtual inheritance existed in C++.)

  • Object delete. (unregistered) in reply to bradwood

    Do you also know El Camino Bignum? What was named with this, why was it named with this, and what was the real name? Oh yes, pun intended. :-)

Leave a comment on “Virtually Careful”

Log In or post as a guest

Replying to comment #:

« Return to Article