S
slocum
Is it possible to create static virtual method in a class ???
slocum said:Is it possible to create static virtual method in a class ???
Is it possible to create static virtual method in a class ???
slocum said:Is it possible to create static virtual method in a class ???
Christian said: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
Static and virtual are incompartible concepts.
Victor said: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.
Christian said:Victor said: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.
Could you be a bit more specific? I'd like to learn more about this, but
a search for "static virtual group:comp.lang.c++" in Google Groups
yields 8.310 results, and I cannot really find anything that's contrary
to what I said.
In fact, the fourth hit [1] is a thread in which it's you who says:
"Access to a virtual function is resolved through an instance of the
class. That's the essence of virtuality of member functions. How, I am
asking, is the compiler going to resolve the access if the instance is
NOT to be used? It seems rather a contradiction to me..."
[1]
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/4a550c5b62d0a988/974bf6c3e04013f1
Fei said:Typically one models this behavior (static virtual) through a static
method that calls a virtual method with argument that contains a
polymorphic object.
Not really, in pseudo code:Christian said:Is that a polymorphic object of the same class hierarchy the class
containing the static method belongs to?
In other words, something along the lines of:
class Base
{
//...
virtual void func() = 0;
static Base *strategy_;
static void staticFunc()
{
strategy_->func();
}
};
class Derived : public Base
{
//...
virtual void func();
};
Base *ptr = new Derived;
Base::strategy_ = ptr;
Base::staticFunc(); // "virtual" static function call
Fei said:A::static_method(poly_object * obj){
do1();
obj->some_method();
do2();
}
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.