Hi!
Vinay Doma said:
function?
This is not legal code. virtual functions cannot be declared as static.
That is a correct statement...
From the MSDN - The virtual function mechanism relies on the specific
object that calls the function to determine which virtual function is
used. Since this is not possible for static functions, they cannot be
declared as virtual.
.... but the explanation isn't. Virtual method binding and
invocation are unrelated concepts. One uses the type
of an object, where an instance of the object may or may
not exist; the other uses the instance itself of the object,
which may [for non-static methods] or may not [for
static methods] exist. It is quite possible to see uses
for static virtual method, where an instance of an object
is used to find the version of the static method, but which
method does not use that instance.
If there were a "typeof" operator that would dynamically
resolve the real type of an object at run-time, the following
hypothetical code would work as the "static virtual" might
be implemented:
struct Base
{
static /* "virtual" */ void F();
};
struct Derived : public Base
{
static /* "virtual" */ void F();
}
void G( Base &r )
{
// Note: 'r' is a reference to a polymorphic type, and can
// therefore refer to an instance of the derived class
dynamic_typeof<r>::F(); /* equivalent to r.F(); if static virtuals
existed */
}
Again, that was not really C++ as it currently exists.
Search Google newsgroups section for "static virtual" to
learn more.
Cheers!
- Risto -
P.S. The real reason for the omission of "static virtual" was
originally an oversight, and more recently lack of demand
and/or agreement.