A use for multiple inheritance?

后端 未结 12 1874
暖寄归人
暖寄归人 2020-11-29 07:29

Can anyone think of any situation to use multiple inheritance? Every case I can think of can be solved by the method operator

AnotherClass() { return this-&         


        
相关标签:
12条回答
  • 2020-11-29 07:51

    It is true that composition of an interface (Java or C# like) plus forwarding to a helper can emulate many of the common uses of multiple inheritance (notably mixins). However this is done at the cost of that forwarding code being repeated (and violating DRY).

    MI does open a number of difficult areas, and more recently some language designers have taken decisions that the potential pitfalls of MI outweigh the benefits.

    Similarly one can argue against generics (heterogeneous containers do work, loops can be replaced with (tail) recursion) and almost any other feature of programming languages. Just because it is possible to work without a feature does not mean that that feature is valueless or cannot help to effectively express solutions.

    A rich diversity of languages, and language families makes it easier for us as developers to pick good tools that solve the business problem at hand. My toolbox contains many items I rarely use, but on those occasions I do not want to treat everything as a nail.

    0 讨论(0)
  • 2020-11-29 07:52

    One case I worked on recently involved network enabled label printers. We need to print labels, so we have a class LabelPrinter. This class has virtual calls for printing several different labels. I also have a generic class for TCP/IP connected things, which can connect, send and receive. So, when I needed to implement a printer, it inherited from both the LabelPrinter class and the TcpIpConnector class.

    0 讨论(0)
  • 2020-11-29 07:55

    I find multiple inheritance particularly useful when using mixin classes.

    As stated in Wikipedia:

    In object-oriented programming languages, a mixin is a class that provides a certain functionality to be inherited by a subclass, but is not meant to stand alone.

    An example of how our product uses mixin classes is for configuration save and restore purposes. There is an abstract mixin class which defines a set of pure virtual methods. Any class which is saveable inherits from the save/restore mixin class which automatically gives them the appropriate save/restore functionality.

    But they may also inherit from other classes as part of their normal class structure, so it is quite common for these classes to use multiple inheritance in this respect.

    An example of multiple inheritance:

    class Animal
    {
       virtual void KeepCool() const = 0;
    }
    
    class Vertebrate
    {
       virtual void BendSpine() { };
    }
    
    
    class Dog : public Animal, public Vertebrate
    {
       void KeepCool() { Pant(); }
    }
    

    What is most important when doing any form of public inheritance (single or multiple) is to respect the is a relationship. A class should only inherit from one or more classes if it "is" one of those objects. If it simply "contains" one of those objects, aggregation or composition should be used instead.

    The example above is well structured because a dog is an animal, and also a vertebrate.

    0 讨论(0)
  • 2020-11-29 07:57

    I think fmsf example is a bad idea. A car is not a tire or an engine. You should be using composition for that.

    MI (of implementation or interface) can be used to add functionality. These are often called mixin classes.. Imagine you have a GUI. There is view class that handles drawing and a Drag&Drop class that handles dragging. If you have an object that does both you would have a class like

    class DropTarget{
     public void Drop(DropItem & itemBeingDropped);
    ...
    }
    
    class View{
      public void Draw();
    ...
    }
    
    /* View you can drop items on */
    class DropView:View,DropTarget{
    
    }
    
    0 讨论(0)
  • 2020-11-29 07:58

    Java has interfaces. C++ has not.

    Therefore, multiple inheritance can be used to emulate the interface feature. If you're a C# and Java programmer, every time you use a class that extends a base class but also implements a few interfaces, you are sort of admitting multiple inheritance can be useful in some situations.

    0 讨论(0)
  • 2020-11-29 08:01

    Most uses of full scale Multiple inheritance are for mixins. As an example:

    class DraggableWindow : Window, Draggable { }
    class SkinnableWindow : Window, Skinnable { }
    class DraggableSkinnableWindow : Window, Draggable, Skinnable { }
    

    etc...

    In most cases, it's best to use multiple inheritance to do strictly interface inheritance.

    class DraggableWindow : Window, IDraggable { }
    

    Then you implement the IDraggable interface in your DraggableWindow class. It's WAY too hard to write good mixin classes.

    The benefit of the MI approach (even if you are only using Interface MI) is that you can then treat all kinds of different Windows as Window objects, but have the flexibility to create things that would not be possible (or more difficult) with single inheritance.

    For example, in many class frameworks you see something like this:

    class Control { }
    class Window : Control { }
    class Textbox : Control { }
    

    Now, suppose you wanted a Textbox with Window characteristics? Like being dragable, having a titlebar, etc... You could do something like this:

    class WindowedTextbox : Control, IWindow, ITexbox { }
    

    In the single inheritance model, you can't easily inherit from both Window and Textbox without having some problems with duplicate Control objects and other kinds of problems. You can also treat a WindowedTextbox as a Window, a Textbox, or a Control.

    Also, to address your .anotherClass() idiom, .anotherClass() returns a different object, while multiple inheritance allows the same object to be used for different purposes.

    0 讨论(0)
提交回复
热议问题