Inheritance and arrays

Rene Zwanenburg renezwanenburg at gmail.com
Mon Jul 3 11:03:28 UTC 2023


On Monday, 3 July 2023 at 09:50:20 UTC, Arafel wrote:
> Is this a conscious design decision (if so, why?), or just a 
> leak of some implementation detail, but that could eventually 
> be made to work?

Besides the pointer adjustment problem mentioned by 
FeepingCreature, it's an unsound conversion even with just class 
inheritance. Consider:

```
class A {}
class B : A {}
class C : A {}

void main()
{
   auto bArr = [new B()];
   A[] aArr = bArr; // If this was allowed..
   aArr[0] = new C(); // This would be a problem, because bArray 
would now contain a C.
}
```


More information about the Digitalmars-d-learn mailing list