| re: static virtual function
"Sjoerd A. Schreuder" <sa_schreuder@wanadoo.nl> wrote in message
news:41dc98fe$0$67309$a344fe98@news.wanadoo.nl...[color=blue]
> Mike Wahler wrote:[color=green]
>> "Dumitru Sipos" <dumitru.sipos@gmail.com> wrote in message
>> news:1104930790.279965@slbhw0...[color=darkred]
>>>
>>>is there possible to have a function that is both static and virtual?[/color]
>>
>> If you study the language definitions of what static member functions
>> and virtual member functions are, you should realize your question
>> does not make much sense.[/color]
>
> It can make sense! RTTI typically doesn't depend on the this-pointer
> (static) while RTTI typically depends on the dynamic type (virtual).
>[/color]
That's not correct. Calling a virtual function involves using the dynamic
(run-time) type of a specific object, which most certainly requires use of
the "this" poiinter. The "this" pointer is not static in any sense that I
know of, but rather is (or at least can be thought of as) a "hidden"
parameter to non-static member function calls.
You say RTTI depends on the dynamic type. Well, the dynamic type..of what?
It has to be the dynamic type of a specific object, and that information is
passed to the function call via the "this" pointer. A static function, on
the other hand, receives no such information, because it does not require a
specific object. So, how could it resolve the dynamic type if it doesn't
have an instance of the object from which to determine the correct type?
You show (below) some function definitions, but how would you call them
without a specific object? What information would be used to resolve which
override is to be called?
[color=blue]
> class Baseobject
> {
> virtual static std::string getCodeAuthor() = 0;
> }
>
> class A : BaseObject
> {
> virtual static std::string getCodeAuthor() { return "Sjoerd"; }
> }
>
> class B : BaseObject
> {
> virtual static std::string getCodeAuthor() { return "Mike"; }
> }
>
> Maybe there are reasons why "static virtuals" should not be allowed,
> but "it doesn't make sense" is certainly not one of them.
>[/color]
It is the primary reason. A call to a static function does not include
reference to any specific object. A call to a virtual function does. The
two are incompatible.
[color=blue]
> Note: I'm quite aware of the fact that C++ doesn't allow
> static virtuals, and of the fact that there is an easy work-around
> by using regular virtual functions.
>[/color]
That's not a work-around for the problem, because there is no problem to
work around. Using "regular" virtual functions allows you to obtain correct
dynamic (polymorphic) behavior when using derived classes. Using static
functions allows you to call member functions without any object (instance)
of the specified class. Two different issues, two different solutions.
I'd be interested in hearing if there's any reason you'd even *want* to have
a static virtual function. Perhaps an example using the functions above...?
-Howard |