A use for multiple inheritance?

后端 未结 12 1941
暖寄归人
暖寄归人 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: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{
    
    }
    

提交回复
热议问题