Emulating Multiple Inheritance

I often miss the MI from C++, but I have a suggestion of how to emulate it in C#, given the following:

interface IFoo
{
void FooIt();
}

class Foo : IFoo
{
public void FooIt()
{
Console.WriteLine("Foo-ing!");
}
}

interface IBar
{
void BarIt();
}

class Bar : IBar
{
public void BarIt()
{
Console.WriteLine("Bar-ing!");
}
}

class BaseClass
{
// More BaseClass methods here.
}

One could use a new construct:

class Baz : BaseClass, Foo as IFoo, Bar as IBar
{
// More Baz methods here.
}

And that would be the same as writing the following forwarding methods:

class Baz : BaseClass, IFoo, IBar
{
private Foo __foo_unique_identifier = new Foo();
private Bar __bar_unique_identifier = new Bar();

void IFoo.FooIt()
{
__foo_unique_identifier.FooIt();
}

void IBar.BarIt()
{
__bar_unique_identifier.BarIt();
}

// More Baz methods here.
}

Wouldn't that be useful

Regards,
Anders


Answer this question

Emulating Multiple Inheritance

  • Chat Hong

    OK, I see, you talk about aggregation.

    1. Can you give example from real life

    2. What if I have two classes that implements IList interface and both of them used inside MyMultiConcrete Which interface implementation will be used



  • ggciubuc

    Besides, A class that inherits from a base or abstract class is only an extention of that class's functionality. Even the inheritance of an interface is strictly a contract that seeks to provide the functionality of the original interface.

    It seems like multiple inheritance leads to classes that aren't very specific in what they extend or what they should do.


  • ZnZn

    Hi!

    I think it should be as it now. Multiple inheritance is an interesting idea, but in practice you have to concern too much about it. Most people do not understand and use MI in C++ (especially with virtual base classes) - it's too complex and give more chances to spent a day in debugger.

    Also MI means more complex compiler and probably less performance in final apps (don't remember details right now).

    MI typically used to mix in class some functionality. This is easy implemented by inserting instance of some functionality. Like that

    class Baz
    {

    public readonly Foo Foo = new Foo();

    public readonly Bar Bar = new Bar();

    }

     

    Can you provide sample from real life when MI are better than this simple design

     

    In this case you have class hierarchy that easier to understand by wider range of people. So the real question is what is more important - give a very complex tool to use by limited range of people or let wider range of developers work together. I think MS choose second and it's right.



  • GARRY TURNER

    Hi Sergey,

    I don't think you understand what I am proposing. I am not proposing to support MI, I am proposing to emulate support for MI using interfaces and automatic forwarding to the interface methods.

    Anyway, someone else has already thought of that proposal anyway, as Andrey Shchekin writes on http://davidkean.net/archive/2005/01/02/216.aspx:
    It is interesting that Comega has a special (undocumented) syntax for that purpose:

    public class MyMultiConcrete : MyBaseClass, IAddOnClass
    {
    private AddOnClass _AddOnClass implements IAddOnClass;

    public MyMultiConcrete()
    {
    _AddOnClass = new AddOnClass();
    }
    }

    Regards,
    Anders

  • Emulating Multiple Inheritance