delegate bug?

Jack Applegame japplegame at gmail.com
Fri Nov 9 12:13:25 PST 2012


Ok. I will try to explain what exactly i need.

import std.stdio;

import std.stdio;

class Figure {
   void draw(){}
   void erase(){}
}

class Circle : Figure {
   override void draw() { writeln("drawing circle"); }
   override void erase() { writeln("erasing circle"); }
}

class Square : Figure {
   override void draw() { writeln("drawing square"); }
   override void erase() { writeln("erasing square"); }
}

class Triangle : Figure {
   override void draw() { writeln("drawing triangle"); }
   override void erase() { writeln("erasing triangle"); }
}

void main() {
   Figure[] figures;
   createFigures(figures);
   doAction(figures, &Figure.draw);
   doAction(figures, &Figure.erase);
}

void doAction(Figure[] figures, void function() action) {
   foreach(Figure figure; figures) {
     // how to do action with figure???
   }
}

void createFigures(ref Figure[] figures) {
   figures ~= new Circle;
   figures ~= new Square;
   figures ~= new Triangle;
}



More information about the Digitalmars-d-learn mailing list