Array type conversion

Mark Burnett unstained at gmail.com
Sat Apr 28 10:01:50 PDT 2007


torhu Wrote:

> Mark Burnett wrote:
> Cline, 
> [http://www.parashift.com/c++-faq-lite/proper-inheritance.html#faq-21.4] 
> this is dangerous.  Is this possibly a holdover from c++?  It is 
> explicitly mentioned in the array page that they behave this way, so I 
> am not convinced that is the case.
> 
> I guess you're already aware that objects in D are reference types, so 
> the specific problem mentioned on that page does not apply?  I you want 
> value types, you would use structs, which cannot be subclassed.

Right the specific problem of size differences in c++ does not exist (Orange has extra data members to demonstrate this), but the "a container of derived is not a container of base" problem still does.

Imagine adding a core function to Apple (and not to Orange of course), then after an Orange is added to the array, you loop go through and core all the Apples..only one of them isn't an Apple and you have undefined behavior.

The attached file again compiles with gdc, and crashes at runtime (though of course it could do almost anything).
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fruit2.d
Type: application/octet-stream
Size: 1309 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20070428/bcdf9d21/attachment.obj>


More information about the Digitalmars-d mailing list