Should this always work?
Steven Schveighoffer
schveiguy at gmail.com
Tue May 4 14:04:54 UTC 2021
On 5/4/21 6:03 AM, Ola Fosheim Grøstad wrote:
> On Tuesday, 4 May 2021 at 01:20:15 UTC, Q. Schroll wrote:
>> On Saturday, 1 May 2021 at 06:17:36 UTC, Mike Parker wrote:
>>> On Saturday, 1 May 2021 at 04:55:10 UTC, frame wrote:
>>>> I always thought as long as an object implements an interface, it
>>>> should be able to cast it from a void* if it really points to a
>>>> supporting object.
>>>
>>> No. An interface is like a pointer to a pointer.
>>
>> Can you elaborate on this one? I don't really get it. Is an object
>> handle also like a pointer to a pointer? (I feel like I can learn
>> something here.)
>
> Off the top of my head the object layout is something like this:
> {
> pointer to vtable0 for the class;
> monitor mutex stuff;
> pointer to interface 1 vtable1;
> pointer to interface 2 vtable2;
> ...
> pointer to interface N vtableN;
> object data 1;
> object data 2;
> ...
> }
>
>
> A pointer to interface 1 is a pointer to the pointer to vtable1 in the
> object above.
>
Yes, this is exactly how it is. See the ABI document:
https://dlang.org/spec/abi.html#classes
So a class reference is *also* a pointer to a pointer (to the vtable),
and a interface reference is the same. However, the slight difference
is, the pointer to the interface doesn't allow useful mechanisms until
you apply the offset (necessitating a double indirection), whereas a
class pointer (combined with compile-time type knowledge) the compiler
can access member fields of the class.
-Steve
More information about the Digitalmars-d-learn
mailing list