how can i call member function from Callback method

Started by
8 comments, last by Stereo 15 years, 1 month ago
here i have 2 call member function like this condition only is class constructor should not call means (Class obj ; obj.doit()) i want to do it by pointer is it possible ? class::doit() { } result callback LLKeyboardProc( int nCode,WPARAM wParam,LPARAM lParam ) { // I HAVE TO CALL doit() } // this is not member function
Advertisement
Nitpick:
that code snippet is definitely not legal in C++: "class" is a reserved keyword, class::doit has no return type specified.
Point 1: This sounds like a homework assignment. If yes, maybe there's a hint in the notes you took during class/lecture? You did take notes, didn't you?
Point 2: (This is a hint) You omitted the class definition. We might help you if you showed us.
Point 3: What exactly do you want/have to do? If you want to call a method of an object, that object has to exist in the first place. That implies that it was constructed somewhere earlier, and that a class constructor was called before. Or is there a hidden assumption in my reasoning that might fail under special circumstances (This is another hint)?
you focusing on the things thats not important try to solve the main prb this is not the class level asignment
i dnt think bother about return type and that things its only dummmy code to understand prb
i wnt to do it by pointer not by class object
take a look at boost's function and signal objects.
(I will read this as "No, it's not homework".)
Then what about
// this is somewhere else in your codeMyClass *obj;// this is the callbackresult callback LLKeyboardProc( int nCode,WPARAM wParam,LPARAM lParam ){   obj->doit();}

Obviously, somewhere else you will have to call
   obj=new MyClass();

or you will get a runtime error.
However, if your class definition looks like
class MyClass {   public:   static void doit();};

(Which I was hinting at in my first post, by the way, TWICE, in fact)
then you can and must write (and maybe that's what you were asking for

result callback LLKeyboardProc( int nCode,WPARAM wParam,LPARAM lParam ){   MyClass::doit();}


I hope my snippets are correct and are helpful to you.
Do you want to do it without globals? Here's an article about that: Thunking in Win32: Simplifying callbacks to non-static member functions
thanx dude its now helpful
:)

real structure of product is Mainfrsm with 5 dialog and with keyhook so its some what hard studff to manage

thanx to all dear frz
Most Win32 functions that involve callbacks allow you to specify some data that gets attached to the callback on a one-to-one basis. You can then attach to the callback a pointer to the class instance to which you want to forward the call, and simply retrieve the class instance from within the non-member callback function right before you forward.

An example with the basic window message processing function here.
SetWindowsHookEx which the OP is most likely using (for a low-level keyboard hook) is a lovely exception to this rule. You gotta love inconsistent API designs ...

This topic is closed to new replies.

Advertisement