static virtual method

Discussion in 'C++' started by slocum, Apr 9, 2008.

  1. slocum

    slocum Guest

    Is it possible to create static virtual method in a class ???
     
    slocum, Apr 9, 2008
    #1
    1. Advertising

  2. slocum

    Jim Langston Guest

    "slocum" <> wrote in message
    news:...
    > Is it possible to create static virtual method in a class ???


    No.

    --
    Jim Langston
     
    Jim Langston, Apr 9, 2008
    #2
    1. Advertising

  3. slocum

    Guest

    On Apr 9, 8:05 am, slocum <> wrote:
    > Is it possible to create static virtual method in a class ???


    No, but what are you trying to do? I'm sure folks can suggest
    ways to accomplish it.
     
    , Apr 9, 2008
    #3
  4. 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.


    --
    Christian Hackl
     
    Christian Hackl, Apr 9, 2008
    #4
  5. Christian Hackl wrote:

    > 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


    I meant to say "because ptr2 points to a Derived2 object", of course.

    > Static and virtual are incompartible concepts.


    And I also meant to say "incompatible" instead of "incompartible" :)


    --
    Christian Hackl
     
    Christian Hackl, Apr 9, 2008
    #5
  6. Victor Bazarov wrote:

    > 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

    --
    Christian Hackl
     
    Christian Hackl, Apr 9, 2008
    #6
  7. slocum

    Fei Liu Guest

    Christian Hackl wrote:
    > Victor Bazarov wrote:
    >
    >> 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
    >
    >


    Typically one models this behavior (static virtual) through a static
    method that calls a virtual method with argument that contains a
    polymorphic object. This is sometimes called a 'Strategy' design pattern.

    Think of it this way, the compiler cannot delegate a static method but
    there is nothing to stop a developer adding delegation in a static method.

    Fei
     
    Fei Liu, Apr 9, 2008
    #7
  8. Fei Liu wrote:

    > Typically one models this behavior (static virtual) through a static
    > method that calls a virtual method with argument that contains a
    > polymorphic object.


    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


    --
    Christian Hackl
     
    Christian Hackl, Apr 9, 2008
    #8
  9. slocum

    Fei Liu Guest

    Christian Hackl wrote:
    > Fei Liu wrote:
    >
    >> Typically one models this behavior (static virtual) through a static
    >> method that calls a virtual method with argument that contains a
    >> polymorphic object.

    >
    > 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
    >
    >

    Not really, in pseudo code:

    A::static_method(poly_object * obj){
    do1();
    obj->some_method();
    do2();
    }

    Of course, this also nicely uses the 'template method' pattern, your
    static_method defines a template method that all callers will follow,
    then some_method can implement polymorphic behavior on demand.

    Fei
     
    Fei Liu, Apr 9, 2008
    #9
  10. slocum

    slocum Guest

    I understand that implementing virtual static method is "technicaly"
    impossible, but I not asked about c++ standart but how to create class
    method that will be behave like virutal static.

    I've found the resolve using templates.

    class A
    {
    public:
    static X();
    };

    class B
    {
    public:
    static X();
    };

    template <class T>
    class C
    {
    public:
    void func (void) {T::X();}
    }

    So I have static X() method and I can switch the C::func() behaving.
    Of course there is no polymorphic mechanism and can't use it in the
    runtime, but this is what I was looking for.
     
    slocum, Apr 10, 2008
    #10
  11. Fei Liu wrote:

    > A::static_method(poly_object * obj){


    Should this perhaps be

    A::static_method(A *obj)

    ?

    > do1();
    > obj->some_method();
    > do2();
    > }


    Otherwise, it certainly looks like an interesting technique, though I am
    not sure I can think of a situation in which it would be very useful.


    --
    Christian Hackl
     
    Christian Hackl, Apr 10, 2008
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    Why do static and non-static method names collide?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 2, 2003, in forum: Java
    Replies:
    12
    Views:
    5,664
    cgbusch
    Jul 5, 2003
  2. Calvin Lai
    Replies:
    7
    Views:
    558
    Calvin Lai
    Dec 18, 2003
  3. jlopes
    Replies:
    7
    Views:
    429
    jlopes
    Nov 19, 2004
  4. Tony Johansson

    why not virtual static method

    Tony Johansson, Aug 21, 2005, in forum: C++
    Replies:
    2
    Views:
    311
    Bob Hairgrove
    Aug 21, 2005
  5. a
    Replies:
    7
    Views:
    367
    dasjotre
    Jun 28, 2007
Loading...

Share This Page