![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Aug 2008
Posts: 1
| Delegate function implementation in C++ for class callback invocation I have created wrapper classes around Win32 API calls in GNU C++ (v 4.3.1) using Win32API library (v 3.11). I am having difficulty in the implementation of API functions that require callback functions as arguments (presumably since I have declared the callback functions as member functions of the class instead of in the global scope and have not implemented the c++ equivalent of a delegate function). Here is a simple example using the EnumWindows Win32 API call: Code: class MyClass {
public:
void AccessorFunc() {
EnumWindows(this->EnumWindowsProc, 0);
}
private:
BOOL CALLBACK MyClass::EnumWindowsProc(HWND hWnd, LPARAM lParam) {
// do whatever...
}
};
Code: In member function 'void MyClass::AccessorFunc()' argument type 'BOOL (MyClass::)(HWND__*,LPARAM)' does not match 'BOOL (*)(HWND__*,LPARAM)' I'm still working on getting up-to-speed on the c++ implementation of the delegate function, though I would like to avoid the use of additional functions outside the class scope in preference of an inline solution, if feasible. At this point, any suggestions are welcome to get the code to compile. Thanks! |
| just_me is offline | |
| | #2 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,783
| There are two things here: First, this->EnumWindowProc is illegal syntax. The standard say to create a function pointer to a member function, you use the syntax &Class::MemberFunction. Second, the problem is that you cannot use a class member function as a callback (how would the function know what instance the function belongs to?). You will have to either create a static member function or a global function. The usual approach is also to pass "this" as the lparam, and to use it to call a non-static member function.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
![]() |
| Tags |
| callback, delegate |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Seg Fault in Compare Function | tytelizgal | C Programming | 1 | 10-25-2008 03:06 PM |
| How do wait till a callback function returns | poorkoder | C Programming | 1 | 06-16-2008 11:22 AM |
| How to fix misaligned assignment statements in the source code? | biggyK | C++ Programming | 28 | 07-16-2006 11:35 PM |
| <Gulp> | kryptkat | Windows Programming | 7 | 01-14-2006 01:03 PM |
| Pointer to member function as callback function | ninebit | C++ Programming | 10 | 03-01-2002 05:52 AM |