Virtual... static method??

Started by
7 comments, last by Qw3r7yU10p! 18 years, 12 months ago
I would like to have a virtual static method, but have read that it is impossible. Although I have heard of a virtual static idiom, which is meant to be like what I am looking for. The only problem is I cannot find any information on this. Does anyone have a link, or can tell me more?
____________________________________________________________Programmers Resource Central
Advertisement
A quick search on google brought up this which seems pretty straight forward.

What exactly are you trying to accomplish?
Quote:Original post by Tera_Dragon
I would like to have a virtual static method, but have read that it is impossible. Although I have heard of a virtual static idiom, which is meant to be like what I am looking for. The only problem is I cannot find any information on this. Does anyone have a link, or can tell me more?
I have never heard of it either, so I searched for "virtual static idiom" on [google]. Not a very widely discussed topic obviously [smile], I only got 2 or 3 results. The first was this.

edit: Beaten to the punch!
That wiki seems a bit silly. All they have done is made the this pointer a visible object. No point in that function being static at all.
As I said I have looked, and had found that site, but it won't load:
Forbidden
You don't have permission to access /cgi/wiki on this server.


What I want is a base class with a virtual static method. The method is to register the class with the angelscript VM.

Edit: It looks like it is only me that it doesn't work for :( Could someone post the contents of the site? If it's of any use that is.
____________________________________________________________Programmers Resource Central
Here it is:

The Virtual Static Idiom is a very simple technique used in C++ to make static methods virtual. This way, static methods can be overridden by subclasses.

Actually, it just binds a static (class) method to an instance by explicitly passing the this pointer. This is more powerful than allowing polymorphic static methods because it makes the static method really a part of an instance.

Note how because the static method is part of the class Foo, it has access to all parts of Foo.

The example below is particularly useful when you have to pass a callback function into a system call, typical practice in Windows. You get one thunk that takes one void * as its parameters.

It can also be useful to explicitly type the incoming this pointer to a Foo *.

 class Foo { public:     Foo(); protected:     // Virtual static idiom <-- StaticCallback?()     virtual bool Callback(); private:     // Virtual static idiom --> Callback()     static bool StaticCallback?( void *pThis )     {         assert( pThis );         return ((Foo *)pThis)->Callback();     } };
Tera_dragon, you are not going to get a true static function that can be called without an instance of the class to behave polymorphically. How would that even work in C++?

The wiki simply states

class Foo { public:     Foo(); protected:     // Virtual static idiom <-- StaticCallback?()     virtual bool Callback(); private:     // Virtual static idiom --> Callback()     static bool StaticCallback?( void *pThis )     {         assert( pThis );         return ((Foo *)pThis)->Callback();     } };


That may be of some use to you, but arguably there is no point in the function being static, or indeed a member of the class.
Thanks all round guys [smile] rate++
____________________________________________________________Programmers Resource Central
class Base {    void static CallVirtualFunction(Base* obj) {        obj->Function();    }    void virtual Function() {        //etc    }};


This is useful because you might have a program which only wants to take C style pointers to functions (as some Windows API calls do). Static member functions have C-style linkage but non-static member functions dont' (including virtual functions).

What if the function you really want to pass is virtual (which can't be made static)? Usually the function taking a function pointer also takes a pointer to some user defined data, which you have to cast to the correct data type. It's often a void*.

void CallbackFunction(void* userData) {    Base* data = reinterpret_cast<Base*> userData;//we are telling the compiler this will always be true    Base::CallVirtualFunction(data);}

This topic is closed to new replies.

Advertisement