Thread: Delegate function implementation in C++ for class callback invocation

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    1

    Delegate function implementation in C++ for class callback invocation

    Hi -
    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...
            }
    };
    I receive the following compile-time error using the MinGW compiler (version 3.14):

    Code:
    In member function 'void MyClass::AccessorFunc()' argument type 'BOOL (MyClass::)(HWND__*,LPARAM)' does not match 'BOOL (*)(HWND__*,LPARAM)'
    MSDN put together an article on "How to: Implement Callback Functions" with this same scenario using .NET, C# and VC++ frameworks, which makes use of the delegate function.

    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!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. How do wait till a callback function returns
    By poorkoder in forum C Programming
    Replies: 1
    Last Post: 06-16-2008, 11:22 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Pointer to member function as callback function
    By ninebit in forum C++ Programming
    Replies: 10
    Last Post: 03-01-2002, 05:52 AM

Tags for this Thread