Sign in to follow this  

Reconciling C / C++ function pointers in windows

This topic is 4542 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

Conceptually, I've got everything worked out, but the execution is a little disappointing. I'm just trying to write a basic DirectDraw RPG in MSVC .NET and right now I'm writing a generic "gengame" class to base all of my other classes on. gengame manages the window and gamestate, so the trouble comes when I try to use gengame::InitWindow(HINSTANCE & hinstance), because that function assigns a function pointer to the WNDCLASSEX::lpfnWndProc member, but now that I have that in a class (as opposed to global), it won't work because of __stdcall and __thiscall differences that I don't really know how to fix. The code is at home.earthlink.net/~foreverbonded/genesis.zip Can anyone show me the appropriate way to fix this or at least a moderately elegant shortcut? Thanks

Share this post


Link to post
Share on other sites
I've tried to look at your code but it's telling me the page cannot be found. However, if the wndproc function is in a class, I believe you need to make it static, but I'm not sure as you've said you're assigning it a function pointer?

Be easier to see if the code worked, but if the wndproc is now in your class, then try making it static.

Edit: Bit rusty in this field. Haven't coded for a while because of tendinitis, so my apologies if that doesn't work.

Share this post


Link to post
Share on other sites
whoops!

There's a capital G in that filename, so it should be

home.earthlink.net/~foreverbonded/Genesis.zip

the static part actually did resolve my problem, but now it gives me errors that say static member functions cannot contain this pointers! That kinda defeats the purpose of having it in the class, doesn't it? lol

Well, at least I'm a step closer.

Thanks

Share this post


Link to post
Share on other sites
OK I've got the code now.

Quote:
from an article
Every C++ class has an implicit pointer called the this pointer. The this pointer points to the instantiation of the object. The this pointer can be dereferenced to members of the class like any other pointer. Inside of a class declaration the use of the this pointer is implicit...


That means that when you call a function from a class, the 'this' pointer is implicitly used in the parameter list. Example:


class CFoo
{
public:
void foo();
};




When you call foo(); it's really being translated as:


foo(CFoo *this);




Being as static members have file scope, there is no 'this' pointer, as you can call the static member without having instantiated a class. That's where your problem lies. [wink]

Share this post


Link to post
Share on other sites
Quote:
Original post by templewulf
but now it gives me errors that say static member functions cannot contain this pointers! That kinda defeats the purpose of having it in the class, doesn't it? lol


Ok, so you do seem to have some idea of what is going on. However, consider: suppose you have a function pointer to some non-static member function. What do you expect the this pointer to *be*? I.e. how are you going to specify which object to call the function on?

The C++ solution is that pointers to member functions have their own type (er, set of types, since the type depends on the function signature too), which basically explains the __stdcall/__thiscall difference. The "pointer to member function" is actually a sort of structure in memory (this is very implementation dependant!) that somehow represents both a pointer to the necessary code, and the this-pointer that will be used for invoking the code. Thus when you create that "bound" pointer-to-member-function, there is special syntax, and then IIRC special syntax again for invoking the thing. Unfortunately I can't remember the details; [google] "bound pointer to member function".

Share this post


Link to post
Share on other sites

This topic is 4542 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this