Maps and Function Pointers

This is a discussion on Maps and Function Pointers within the C++ Programming forums, part of the General Programming Boards category; Using something along the lines of: Code: typedef int (*CBACK)(int,DWORD); map<int,CBACK> CBFuncs; // developer calls provided functions to add required ...

  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,185
    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, 10:34 AM
  3. Function Pointer Assigned To Map Overload Resolution Problem!
    By DeepBlackMagic in forum C++ Programming
    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, 08:12 AM
  5. maps
    By sami in forum C++ Programming
    Replies: 0
    Last Post: 10-11-2001, 08:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21