Jump to content
  • Advertisement
Sign in to follow this  
Tera_Dragon

Virtual... static method??

This topic is 4868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
A quick search on google brought up this which seems pretty straight forward.

What exactly are you trying to accomplish?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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();
}
};

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!