January 17, 2007 Re: Why can't static functions be virtual | ||||
---|---|---|---|---|
| ||||
Posted in reply to John McAuley | John McAuley wrote:
> Are statics non-virtual because that's the way c++ does it?
>
> Surely the compiler can see base.hello() and then:
>
> * look up the CBase class definition to find hello
> * get its index in the vtable and note that it is static
> * dereference base to get the CDerived vtable
> * get the address that corresponds to hello
> * call hello without passing base as a secret this pointer
What if hello want to call another "static virtual" function? This wouldn't work without a this pointer.
|
January 17, 2007 Re: Why can't static functions be virtual | ||||
---|---|---|---|---|
| ||||
Posted in reply to James Dennett | James Dennett wrote:
> Quite possible, given one of many possible specifications;
> the difficulty is in making such a specification useful
> rather than brittle and confusing. The difficulties with
> the concept of virtual statics *are* conceptual, not
> implementation issues.
>
> If D has one advantage over C++, it is in its relative
> simplicity. Bogging it down with this kind of conceptual
> murkiness won't help it as a language, IMO.
Good points.
However, one significant benefit that would come from virtual statics would be the ability to add static methods to interfaces (I can't tell you how often I've wanted to define a static method in an interface!)
And I think the implementation could be pretty straightforward:
1) Instead of a this pointer, static methods always get a ClassInfo pointer.
2) Each ClassInfo would have to put static methods into a StaticVTable, rather than just calling those methods by offset.
Personally, I'd be in favor of it. I think it'd be pretty useful.
--benji
|
Copyright © 1999-2021 by the D Language Foundation