Thread overview
Without multiples inheritance, how is this done?
May 08, 2021
Jack
May 08, 2021
matheus
May 08, 2021
Adam D. Ruppe
May 08, 2021

let's say I have:

class Base
{
    int f()
    {
        doSomething();
        return n * 5;
    }

    void doSomething() { }
}

class Foo : Base
{
    void myMethod() { /* ... */ }
}

class Baa : Base
{
    void myMethod2() { /* ... */ }
}

then I'd like to make a extended version(making those DRY routines a class itself) of Foo and Baa, like this:

abstract class DRY : Base
{
    this(int n)
    {
        this.n = n;
    }

    override int f()
    {
        super.doSomething();
        return n;
    }

    private int n;
}

but the class ExtendFoo and ExtendedBaa must inherit from Foo and Baa, respectively. But how can I make it inherit the routines from DRY class too without multiples inheritance? in C++ I'd just do:

class ExtendedFoo : DRY, Base { /* ... */ }
class ExtendBaa : DRY, Base { /* ... */ }
May 08, 2021
On Saturday, 8 May 2021 at 18:33:35 UTC, Jack wrote:
> ...
> but the class ExtendFoo and ExtendedBaa  must inherit from Foo and Baa, respectively. But how can I make it inherit the routines from DRY class too without multiples inheritance? in C++ I'd just do:
>
> class ExtendedFoo : DRY, Base { /* ... */ }
> class ExtendBaa : DRY, Base { /* ... */ }

What about this model:

class baseFoo{
   // Implement foo stuff
}

class baseFooBar : baseFoo{
   // Implement bar stuff
}

class FooBar : baseFooBar{
   // Do your FooBar thing!
}

Matheus.
May 08, 2021

On Saturday, 8 May 2021 at 18:33:35 UTC, Jack wrote:

>
abstract class DRY : Base
{
    this(int n)
    {
        this.n = n;
    }

    override int f()
    {
        super.doSomething();
        return n;
    }

    private int n;
}

You can change that from abstract class to mixin template, make Foo and Baa be interface, then use mixin DRY; inside your child class.