Skip to main content
gamedev.net
🔒 Locked

Casting Problem

Started by Antorian 7 replies 350+ views
Original Post
Antorian
Antorian
I''m a newbie on C++ and I''ve got a problem with casting. My problem is when I want to create A windows in OpenGL context I must Specify the WNDPROC callback procedure to manage the system messages. So my callback must access the field of a class, so it''s included in my class: LRESULT CALLBACK MyClass::WndProc(....); and when I register my window class wc.lpfnWndProc = (WNDPROC)WndProc; It occurs a link error (with VC6): [ error C2440: ''='' : cannot convert from ''long (__stdcall MyClass::*)(struct HWND__ *,unsigned int,unsigned int,long)'' to ''long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)'' There is no context in which this conversion is possible ] So if I put the Callback as a friend of MyClass the link error is no more, but I cannot access datas from my object instance. Can someone tell me how to do, and if I wrong to do like that. Is there a special casting to do? Thanks For ALL I''''m 4 & I draw
I''m 4 & I draw
Kern
Kern
You won''t be able to access your class data anyway, what you should do is make the callback function static, this keep it in your class but it won''t have a this* in it so you can link it to your window.

If you want to access your class data when checking the message, I suggest you do like this:
WinProc => I get a setfocus so I send a getfocus message
getfocus is a user defined message that I look for in the checkmessage function
CheckMessage => I get a getfocus so I know a setfocus just occured.
Antorian
Antorian
Oui mais...
Yes of course the static option allow to link but I cannot Access Class data anyway....

What I don''t understand, is why it''s not possible to link a callback that is a class method.
My goal is to get messages and put them in ''char keys[256]'' array to chack in my object when a key is pressed.
Also I must resize my scene (in OpenGL) when resize message occurs.
So if I make several instance of my object (MyClass) I would like to manage for each instance the system messages.

Have you any idea to do that?

Thanks for all

Kern: merci pour l''info

I''''m 4 & I draw
I''m 4 & I draw
Zipster
Zipster
quote:
What I don''t understand, is why it''s not possible to link a callback that is a class method.

Class methods have something known as a context. I''m sure you''ve heard of it, it''s the this pointer. It allows the function to operate on different sets of data, depending on which class instance called the function. However, global functions like WndProc don''t have context. When you try to cast a member function to a global function, you are trying to strip away the context of the member function, something which you know it vital towards the operation of member functions. The compiler simply won''t let you cast away the context, because it is necessary. Heck, if you tried to run a member function with a NULL context, you''d probably crash

As AP mentioned as I was typing my post, the context is an invisible extra parameter, although that''s not really a standard so it might not be true in all cases.
Fruny
Fruny
See ''function pointer'' link in sig.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Antorian
Antorian
I understand now
It''s just a bad point of vue over the Class Method Context I was having.
Thanks you for lighting me.
Great It runs perfectly now.
I''ve just Limited My app to run with is own graphic context (wich suits better with Oriented Object).

It was as if I would like go trough a wall without seing It
But Now I see.

Thanks.
Good People on good Forum.



I''''m 4 & I draw
I''m 4 & I draw

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.