Thread: Callback type things

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    Callback type things

    I have this code currently:

    Code:
    void (*OnPaint)(WindowArgs, PaintArgs);
    It works like I want it to, the user creates a void function with the arguments it says there (they are alreayd defined structs) and than the user simple does
    Code:
    OnPaint = func_name;
    And they decalre the function like this:

    Code:
    void paint(WindowArgs args, PaintArgs paint)
    {
    //Do stuff here
    }
    But the thing is I want the user know what kind of arguments to put inside the callback without looking at the callback code. It is a very vague question so I hope someone can help me. Ask if you need any more info.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you're wondering how to call a function pointer:
    Code:
    void (*funcp1)(int) = function, (*funcp2)(int) = &function;
    
    (*funcp1)(5);
    funcp1(5);
    As you can see, there are two ways to assign and call function pointers.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ahh ok I see, I think I got some ideas now. Thanks.

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    The user can not be completely ignorant of the implementation details, they will have to make a callback function with the specified parameters before you can appropriately pass a function pointer around as a callback, bowever you can ease the complex syntax using typedef's. For example, take the API function BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam); The WNDENUMPROC is a user-defined callback, WNDENUMPROC is typedef'd as "typedef BOOL (CALLBACK* WNDENUMPROC)(HWND, LPARAM);" This means a user would define their callback like:

    Code:
    BOOL MyEnumWindowsCallback(HWND hWnd, LPARAM lParam)
    {
            return 0;
    }
    And then call the function with:

    Code:
    EnumWindows(&MyEnumWindowsProc, someSortOflParamValue);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:35 PM
  3. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  4. odd errors from msvc std library files
    By blight2c in forum C++ Programming
    Replies: 6
    Last Post: 04-30-2002, 12:06 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM