Why can't static functions be virtual

James Dennett jdennett at acm.org
Wed Jan 17 07:46:07 PST 2007


John McAuley wrote:
> Bill Baxter Wrote:
> 
>> The problem is that static methods generally don't have offsets in the 
>> vtable at all, so getting the CDerived vtable doesn't help.
>>
>> --bb
> 
> Change the compiler to write the offsets.

And then what happens when one virtual static calls
another?  There's no "this" pointer in the first,
so it can't use virtual lookup to find the second,
and so it suddenly uses non-virtual lookup as soon
as we insert a trivial forwarding function.  Not
good.  The idea of virtual dispatch is very much
tied to there being an object; the idea of static
methods is very much about there *not* being an
object.  Attempts to make them meet in the middle
cause unpleasant side-effects, and in my opinion
aren't worthwhile.  This has been discussed in the
context of C++ many times, and the issues are the
same as with D in this case.  Even overloading
static with non-static methods is somewhat
problematic, and not because of implementation
issues.

-- James



More information about the Digitalmars-d mailing list