On Tue, 22 Sep 2009 18:56:12 -0400, Christopher Wright
<[email protected]> wrote:
Robert Jacques wrote:
Yes, although classes have hidden vars, which are runtime dependent,
changing the offset. Structs may be embedded in other things (therefore
offset). And then there's the whole slicing from an array issue.
Um, no. Field accesses for class variables are (pointer + offset).
Successive subclasses append their fields to the object, so if you
sliced an object and changed its vtbl pointer, you could get a valid
instance of its superclass.
If the class layout weren't determined at compile time, field accesses
would be as slow as virtual function calls.
Clarification: I meant slicing an array of value types. i.e. if the size
of the value type isn't a multiple of 16, then the alignment will change.
(i.e. float3[])
As for classes, yes the compiler knows, but the point is that you don't
know the size and therefore alignment of your super-class. Worse, it could
change with different run-times or OSes. So trying to manually align
things by introducing spacing vars, etc. is both hard, error-prone and
non-portable.