Why can't a method be virtual AND static at the same time?

ed growlercab at gmail.com
Wed Jan 29 16:30:04 PST 2014


On Wednesday, 29 January 2014 at 15:30:38 UTC, Martin Cejp wrote:

You cannot override the static method because it isn't in the 
vtable and has a different calling convention; no this pointer is 
passed.

> Technically, yes, there would need to be two methods generated 
> because of ABI differences, but this could be done behind the 
> scenes. By making a method both override and static, you'd tell 
> the compiler to do exactly that. Of course, the question is 
> whether this would really be worth implementing and based on 
> the reactions so far, I guess the answer is Not at all. I'm 
> surprised that nobody else misses this feature, though.

So you want to overload the static method. Well you can do that 
(untested but should work):
---

class A {
     static void f() {writeln(__PRETTY_FUNCTION__);}
}
class B : A {
     void f() {writeln(__PRETTY_FUNCTION__);}
}
class C : B {
    // Overrides from B, overloads from A
     override void f() {writeln(__PRETTY_FUNCTION__);}
}
void main () {
  auto b= new B();
  auto c = new C();

  A.f();
  b.f();

  c.f();
}
---
Is this what you're trying to do?

IMO though overloading base class methods should be avoided

Cheers,
ed


More information about the Digitalmars-d mailing list