Array of derived class objects?

simendsjo simendsjo at gmail.com
Tue Mar 6 11:36:47 PST 2012


On Tue, 06 Mar 2012 20:27:56 +0100, H. S. Teoh <hsteoh at quickfur.ath.cx>  
wrote:

> Code:
>
> 	// test.d
> 	class A {}
> 	class B : A {}
> 	class C : A {}
> 	void main() {
> 		A[] objs = [ new B, new C ];	// line 6
> 	}
>
> Compiler error:
>
> 	test.d(6): Error: cannot implicitly convert expression (new B) of type  
> test.A to test.C
> 	test.d(6): Error: cannot implicitly convert expression ([(__error),new  
> C]) of type C[] to A[]
>
> I thought D was supposed to automatically infer the most derived common
> base class to assign to an array literal of different types? At least,
> that's the impression I got from TDPL.
>
> What am I doing wrong?
>
>
> T
>

I have no idea (and haven't read TDPL), but adding a new A somewhere in  
the array makes it work. Compiler bug..?


More information about the Digitalmars-d-learn mailing list