Array initialization with common base - Best way?

deadimp deadimp at gmail.com
Sat Nov 8 22:23:56 PST 2008


What's a 'good' way to initialize an array of references to classes using a common base?
I've tried this and it works:

class Base
{
 void func();
}
class GoodChild : Base
{
 void func();
}
class BadChild : Base
{
 void func();
}
//...
auto list = [cast(Base)new GoodChild, new BadChild, ...];

I had tried doing it without the initial cast, but ran into issues because D (DMD v1.033) would infer the type based off of the first element in the literal, and then assumed that all other elements would be the same. 


More information about the Digitalmars-d-learn mailing list