Array of derived class objects?

Ali Çehreli acehreli at yahoo.com
Tue Mar 6 11:36:36 PST 2012


On 03/06/2012 11:27 AM, H. S. Teoh 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
>

That's probably this bug:

   http://d.puremagic.com/issues/show_bug.cgi?id=5498

Luckily it has a pull request.

Ali


More information about the Digitalmars-d-learn mailing list