Topic: static virtual functions (was: ctors/dtors)


Author: Viktor Yurkovsky <n4mation@panix.com>
Date: 4 Jan 1995 06:37:27 GMT
Raw View
In terms of implementation, static member functions are really fake
members.  They don't get a this pointer and have little to do with
the class they are declared in.

The only reason for using them that I though of so far is for
scoping purposes, to hide their name.

-------------------------------
n4mation@panix.com             |
Victor Yurkovsky               |
                               |
Compiler writer                |
                               |
Special discounts for          |
weddings and funerals.         |
_______________________________|





Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Fri, 30 Dec 1994 12:03:39 GMT
Raw View
tob@world.std.com (Tom O Breton) writes:

>fjh@munta.cs.mu.OZ.AU (Fergus Henderson) writes:
>> It's true that in C++ static member functions can't be virtual, but
>> this is an explicit restriction of C++, not a logical necessity. [...]
>
>Well, yes, if a static gets a pointer or ref to a particular instance it
>could (in theory, not in C++) dispatch polymorphically from that
>instance. But if it does that it's just duplicating ordinary member
>functions.

No, there would be a difference.

   object required? virtual dispatch?
 ordinary fn  YES   NO
 static fn  NO   NO
 virtual fn  YES   YES
 static virtual fn NO   YES

Note that static virtual functions could give you virtual dispatch on the
type *without* requiring you to create an object.

   what hidden argument(s) get passed:
 ordinary fn  pointer to object
 static fn  nothing
 virtual fn  pointer to object and (hence) pointer to vtable
 static virtual fn pointer to vtable

For example, you could use them as follows:

 struct Base {
  static virtual String type_name()
   { return "Base"; }
  static virtual String ptr_type_name()
   { return type_name() + " *"; }
 };
 struct Derived {
  static virtual String type_name()
   { return "Derived"; }
 private:
  Derived();
 };
 main() {
  cout << Derived::ptr_type_name() << endl;
   // prints out "Derived *".
 }

>So while it's not a logical neccessity, there's no reason for it even in
>theory.

I think the above shows that it does conceivably have some interesting uses.

Of course, you also need to consider the pragmatics - is the additional
difficulty of implementation worth it?  I don't know.  Probably not.

--
Fergus Henderson - fjh@munta.cs.mu.oz.au




Author: eyala@applicom.co.il (Eyal Alaluf)
Date: Wed, 4 Jan 1995 13:12:51 GMT
Raw View
It is true that virtual static methods will add functionality. I would also
like to have virtual static members. However, will it not be possible to add
them as part of the RTTI (real time type information) of the object?
Isn't implementing RTTI require implementing virtual static methods and members?
So I think that if it is not part of the suggested RTTI, the cost of adding it
should be minimal.

-----------------------------------------------------------------------------
    Eyal Alaluf                                eyala@applicom.co.il
    Mainsoft Israel Ltd.                       c/o Applicom Systems Ltd.
                                               16 Abba Hillel Silver St.
    Phone : 972 -(3) 575 5550 ext : 1287       Ramat-Gan, 52506
    Fax   : 972 -(3) 751 5906                  Israel