Archived

This topic is now archived and is closed to further replies.

PowerBoyAlfa

How do you typecast a class function pointer to act as base global function?

Recommended Posts

*Reposted from LionHead Studios Discussion Forum Alright here is a tough one. I have a class like so: class MYCLASS { public: MYCLASS(); private: UINT IncMe; long WINAPI ThisFunction(int num); }; Ok, now the ''MYCLASS'' constructor has a function in it that needs a pointer to the ''ThisFunction'' callback function. The first problem is that the because the call is made in the constructor, ''ThisFunction'' is still virtual and needs to be initialized. So I placed this in the global scope: long (WINAPI MYCLASS::*lpThisFunction)(int num)=&MYCLASS::ThisFunction; Which makes a nonvirtual pointer to the class function. My problem now is that the function which needs the pointer to ''ThisFunction'' won''t accept the pointer because it has a conversion error: error C2664: ''lpThisFunction'' : cannot convert parameter from ''long (__stdcall MYCLASS::*)(int)'' to ''long (__stdcall *)(int)'' As you can see, the problem is that the pointer is still associated with the class and doesn''t match what type of parameter is required for the function to accept the pointer. Is there a way to typecast this problem, or is there another way all-together. I realize that this type of action goes against standard OOP, but believe me there is no way around it.

Share this post


Link to post
Share on other sites
1. There could be a problem because you try to store the address of a non static class method. You can only strore the address of a static method (and then you won''t need the element pointer syntax) or you can only store the address of the function from an already instantiated object of the class!

2. In the constructor there won''t happen any "virtual function overloading" (the constructor can''t call a virtual function of an child class because there is no virtual function table at construction time)however you can use the virtual function of the class in the constructor of the class (but don''t expect to call the virtual function of any inherited child!!!)

3. Sometimes you can get around function callbacks while using "function objects" which conatin an TYPE operator()(); operator.

Bjoern

Share this post


Link to post
Share on other sites
RATS! Well I am back were I started then. Thanks anyway, I have some other ideas I will try, and if anybody else has some ideas, I''d be happy to here ''em.

Share this post


Link to post
Share on other sites
Strange list - sometimes it doesn''t show the correct number of replies in the overview...

Try to explain your problem you want to solve (not that you wnt to use a virtual function in a constructor but what you want to solve or reach with it). Perhaps someone else (or I) has stumbled about a related problem and can help you spare some time rewritting things...

Bjoern

Share this post


Link to post
Share on other sites
I''ve run into this problem before. It would have helped if you had explained a little more what you''re doing in your constructor, but I bet I know what''s going on. You are trying to pass a pointer to ThisFunction to a WINAPI function. You want ThisFunction to be the callback function.

To understand what''s not working you need to go down a little bit lower. First of all ThisFunction is not virtual. Next when any function is part of a class it actually has a hidden parameter. The first parameter is the this pointer. Whenever you use a class the compiler translates it to the old C struct * method. Your compiler is still trying to hide this in your error message. It really translates to "cannot convert parameter from ''long (__stdcall *)(MYCLASS *,int)'' to ''long (__stdcall *)(int)."

Basically callback functions can not be part of a class. Most callback functions accept a parameter like LONG userdata. They expect you to cast that into a pointer to your class. But from your example it doesn''t seem to be the case. Unfortunatly your only option is to have a global variable pointing to MYCLASS

Share this post


Link to post
Share on other sites