A use for multiple inheritance?

后端 未结 12 1881
暖寄归人
暖寄归人 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 08:15

    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.

    This example doesn't really illustrate the usefulness of multiple inheritance. What being defined here is an INTERFACE. Multiple inheritance allows you to inherit behavior as well. Which is the point of mixins.

    An example; because of a need to preserve backwards compatibility I have to implement my own serialization methods.

    So every object gets a Read and Store method like this.

    Public Sub Store(ByVal File As IBinaryWriter)
    Public Sub Read(ByVal File As IBinaryReader)
    

    I also want to be able to assign and clone object as well. So I would like this on every object.

    Public Sub Assign(ByVal tObject As )
    Public Function Clone() As 
    

    Now in VB6 I have this code repeated over and over again.

    Public Assign(ByVal tObject As ObjectClass)
        Me.State = tObject.State
    End Sub
    
    Public Function Clone() As ObjectClass
        Dim O As ObjectClass
        Set O = New ObjectClass
        O.State = Me.State
        Set Clone = 0
    End Function
    
    Public Property Get State() As Variant
        StateManager.Clear
        Me.Store StateManager
        State = StateManager.Data
    End Property
    
    Public Property Let State(ByVal RHS As Variant)
        StateManager.Data = RHS
        Me.Read StateManager
    End Property
    

    Note that Statemanager is a stream that read and stores byte arrays.

    This code is repeated dozens of times.

    Now in .NET i am able to get around this by using a combination of generics and inheritance. My object under the .NET version get Assign, Clone, and State when they inherit from MyAppBaseObject. But I don't like the fact that every object inherits from MyAppBaseObject.

    I rather just mix in the the Assign Clone interface AND BEHAVIOR. Better yet mix in separately the Read and Store interface then being able to mix in Assign and Clone. It would be cleaner code in my opinion.

    But the times where I reuse behavior are DWARFED by the time I use Interface. This is because the goal of most object hierarchies are NOT about reusing behavior but precisely defining the relationship between different objects. Which interfaces are designed for. So while it would be nice that C# (or VB.NET) had some ability to do this it isn't a show stopper in my opinion.

    The whole reason that this is even an issue that that C++ fumbled the ball at first when it came to the interface vs inheritance issue. When OOP debuted everybody thought that behavior reuse was the priority. But this proved to be a chimera and only useful for specific circumstances, like making a UI framework.

    Later the idea of mixins (and other related concepts in aspect oriented programming) were developed. Multiple inheritance was found useful in creating mix-ins. But C# was developed just before this was widely recognized. Likely an alternative syntax will be developed to do this.

提交回复
热议问题