Thread: Maps and Function Pointers

  1. #1
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66

    Maps and Function Pointers

    Using something along the lines of:

    Code:
    typedef int (*CBACK)(int,DWORD);
    map<int,CBACK> CBFuncs;
    // developer calls provided functions to add required callback function pointers to
    // map, as they need it
    Now I am familiar with the usage of Maps and how to locate entries, but would I then be able to call the function being pointed by through this map?

    iterCBFuncs->(&second())(myInt, myDWORD);
    I'm quite sure the above wouldn't work, but just trying to show the concept I am getting at. Accessing the pointer to call the actual function. How exactly would this be done, or is it even possible?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, let's see. I'm assuming iterCBFuncs is an iterator on your map, so iterCBFuncs->second would be a function pointer. So (*(iterCBFuncs->second))(myInt, myDWORD) should call it.

    I'm pretty sure you also don't have to dereference a function pointer if you don't want to, so (iterCBFuncs->second)(myInt, myDWORD) should work as well.

  3. #3
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    Oh I must be tired, I was thinking that it was called Iter->second() like a method instead of Iter->second... oi. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing 3 dimentional array into function
    By 182blink in forum C Programming
    Replies: 3
    Last Post: 10-28-2006, 05:20 PM
  2. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  3. Replies: 1
    Last Post: 05-01-2004, 05:41 AM
  4. maps as reference?
    By robquigley in forum C++ Programming
    Replies: 2
    Last Post: 11-26-2003, 09:12 AM
  5. maps
    By sami in forum C++ Programming
    Replies: 0
    Last Post: 10-11-2001, 08:47 AM