Archived

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

Function pointers??

This topic is 6389 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''m traying to use a function pointer who points to the class fonctions. This is a first example of code that works(without casses): void draw2() { return; } void (*cur_function2)() = draw2; and this is a second part that desent work. Why not? Will it ever work? CConsole console; //class void Cconsole::draw() { return; } void (*cur_function)() = console.draw; //err -> ''initializing'' : cannot convert from '''' to ''void (__cdecl *)(void)'' Thanks.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you want a function pointer to a class member, this is the syntax:

    
class CConsole
{
...
public:
void draw() { ... }
};

CConsole console;
void (CConsole::*ptr)() = &console.draw;


(You might not need the & on console.draw.)

Share this post


Link to post
Share on other sites
you could also make the class function a friend like
class someclass{
friend somefunction();

};

someclass::somefunction(){

}
then you can make a function pointer to it I belive, but the post above does it better

Im Always Bored
--Bordem
ICQ: 76947930

Share this post


Link to post
Share on other sites
See C++ converts the member functions into function pointers internally with the first parameter as the pointer to the class itself. You will understand this concept if u try to understand programming DirectX in C and C++ (differenc). In C u use the lpvtbl array.
For the problem the above solutions are fine.

Share this post


Link to post
Share on other sites