Why can't static functions be virtual

John McAuley john_mcauley at bigfoot.com
Wed Jan 17 05:22:06 PST 2007


Boris Kolar Wrote:

> If it worked the way you describe, it would be a little bit slower. The way it
> works now,
> compiler doesn't need to use object reference/pointer at all. It works just as if
> you would
> type "CBase.hello()" (and note that "." does not mean hello is invoked on an
> instance, it's
> just as if "CBase.hello" was a name of a global function).

I'm not saying deprecate calling via classname.funcname. Keep it. 

There is absolutley no reason to get rid of it, and plenty of reasons to keep it.

Its just that if the programmer chooses to call via an object reference then the call should be virtual.

Also:

   class CBase
   {
      static void hello() { printf("hello from CBase"); }
   }

   class CFoo : Cbase
   {
      static void hello() { printf("hello from CFoo"); }
   }

   void function() fn;

   fn = &CBase.hello; // legal, will print hello from CBase if called

   fn = &Cfoo.hello // legal, hello from CFoo

   CBase objectRef = new CFoo;

   // legal, because compiler emits code to chase down address at
   // run time.
   // address has same type as fn
   //
   fn = &objectRef.hello; // calling fn would print hello from CFoo

I'm not worried about any speed issues. If I want to call as fast as possible or I want to be SURE that I call the CBase version then I'd use CBase.hello();. 

If I want virtual goodness then I use objectRef.hello();

The programmer has the choice of how to call.




More information about the Digitalmars-d mailing list