why can't structs implement interfaces?

Bill Baxter wbaxter at gmail.com
Tue Nov 24 15:46:29 PST 2009


On Tue, Nov 24, 2009 at 3:09 PM, Saaa <empty at needmail.com> wrote:

>>> I wanted to do something like this:
>>>
>>> class C : I {};
>>> struct S : I {};
>>> S s;
>>> I[] i =[new C(), s ];
>>
>> Yeh, that's never going to work because that's acting as a dynamic
>> polymorphic interaface.  Referring polymorphically to a struct like
>> that pretty much makes it not a struct anymore, and requires having
>> the hidden pointer to a vtable that was mentioned.  That's what
>> classes are for.
> Why is a hidden pointer necessary? (Just curious :)
>
> My simplistic view was like this:
> i[1] would just hold the location of s and s would be checked to have
> all it needs to be an I.

I think it could be done with a different implementation of interfaces
from the one D uses, one based on "fat pointers".
With that design an I referring to an S would be a "fat pointer", one
pointer pointing to the S and one pointing to S's table of function
pointers (vtable) for the I interface.

That's not how D does it AFAIR, but I don't actually recall how D does it.

--bb


More information about the Digitalmars-d-learn mailing list