Array/list of objects of different type but with same interface

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Wed Jun 20 14:49:51 PDT 2012


On 20/06/12 22:14, Ali Çehreli wrote:
> void main()
> {
>     Agent[] agents;
>     agents ~= new SmartAgent();
>     agents ~= new ConfigurableAgent!("int x;", "int y;");
>     agents ~= new ConfigurableAgent!("double x;", "long y;");
>
>     foreach (agent; agents) {
>         agent.trade();
>     }
> }

Ahh, I hadn't realized that I could create an array whose "type" was the 
interface -- though it makes sense.  Thanks very much!

Is there any way to do this with structs instead of classes?  I ask because in 
my experience the difference in performance is dramatic.


More information about the Digitalmars-d-learn mailing list