multiple inheritance

Timon Gehr timon.gehr at gmx.ch
Sun Jul 8 10:43:27 PDT 2012


On 07/08/2012 07:31 PM, Namespace wrote:
> How can i implement C++ behaviour like this:
>
> class Shape : Drawable, Transformable {
> class Sprite : Drawable {
> class Image : Transformable {
> ?
>
> One way is to declare Transformable or Drawable as interface.
> But what if i have more then one class which implements
> Transformable/Drawable and i wouldn't rewrite the implementation of
> Transformable/Drawable in every class again?

Depending on what exactly your use case is, this might get you going:

class Shape : Drawable {
     protected class MyTransformable : Transformable{
         // ...
         // (can reference all fields of the enclosing class object)
     }

     Transformable transformable;
     alias transformable this;

     this() { transformable = new MyTransformable(); }
     // ...
}


More information about the Digitalmars-d-learn mailing list