Re: static virtual method
Christian Hackl wrote:
slocum wrote:
Is it possible to create static virtual method in a class ???
How could that possibly make sense? Consider this:
class Base
{
public:
virtual void func() = 0;
};
class Derived1 : public Base
{
public:
virtual void func() {}
};
class Derived2 : public Base
{
public:
virtual void func() {}
};
At runtime, which version of func() is called depends on which
*object* is hidden behind a pointer or reference to the base class:
Base *ptr1 = new Derived1;
Base *ptr2 = new Derived2;
ptr1->func(); // Derived1::func() called because ptr1 points to a
// Derived1 object
ptr2->func(); // Derived2::func() called because ptr1 points to a
// Derived2 object
A static method, however, is not called on a particular object. It
does not even need an object of the class to be instantiated. You
would have to specify the class explicitly, for example:
Derived1::staticFunc();
Derived2::staticFunc();
Base::staticFunc(); // does not make sense -- how should the compiler
// know which derived version you mean?
Therefore, the function's virtualness would be completely senseless.
Static and virtual are incompartible concepts.
That's not entirely true. I urge the OP and you to look through
the archives of this and the com.lang.c++.moderated newsgroups to
find that _sometimes_ (not at all often, of course) there *can*
be a need for a static virtual mechanism. Don't get hung up on
the need to have a pointer to the object to call a virtual function
because that would only be required for a non-static VF. Open your
mind a bit and read what has already been said about the subject.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask