C++ function address to as a parameter
Results 1 to 6 of 6

Thread: C++ function address to as a parameter

  1. #1
    Member
    Join Date
    Sep 2003
    Posts
    76

    C++ function address to as a parameter

    Does anybody know if it is possible in C++ to send the address of a function as a parameter to a function.
    Example

    foo( void* a[], int lenght i, int size, int compare(void* uno, void* dos));

    then when i call it i wanna say something like

    foo( numbers, 1000 , sizeof( numbers[0], compare);

    what i wanna do is send to foo the address to where compare is so that it knows where to look at when it says compare inside function foo.


    I know this all seems weird but its an assignment I have to do where I have to pass a code from c++ directly to assembly. Anyways reply for more info or if anybody has heard about something like this.
    I Speak in frequencies even dogs have trouble hearing


  2. #2
    Senior Member
    Join Date
    Jan 2002
    Posts
    1,207
    It's completely possible to send a function as a parameter in C++. The syntax for it is awful, but it is possible.

    Higher order functions rock

    I have a tip: always use a typedef for a function pointer, it makes it much more legible:

    Code:
    // typedef of a "ProcessFunc" which is a function which takes a pid, owner, and two sets of pids
    typedef void (* ProcessFunc) (int pid, int owner, const IntSet &idsin, IntSet &idsout);
    
    static void ForEachProcess( ProcessFunc p, const IntSet &idsin, IntSet &idsout) {
    // function body removed for clarity
    p(pid, mystat.st_uid, idsin, idsout);
    }
    
    // Declaration (function body removed for clarity)
    static void FindRunningFunc(int pid, int owner, const IntSet &idsin, IntSet &idsout);
    
    static void FindRunningUids(IntSet &uids)
    {
    	ForEachProcess(FindRunningFunc, uids, uids);
    }

  3. #3
    Banned
    Join Date
    Nov 2003
    Posts
    182
    He wants to pass a functions address (pointer) to another function as a parameter. And yes it's possible.

  4. #4
    Member
    Join Date
    Sep 2003
    Posts
    76
    Exactly what i want to do is pass the address cause my assignment is to do a direct translation from C++ to MIPS. Funny thing I have it done in MIPS and C++ which is the language I dominate was the one to give me problems. Anyways I think I have an idea so Ill post if it works
    I Speak in frequencies even dogs have trouble hearing


  5. #5
    Member
    Join Date
    Sep 2003
    Posts
    76
    Oh and since I mentioned MIPS, Im wondering if anybody experienced in its use can give me a proper definition of the "lw" and "sw" commands
    What is the difference between loading a word and storing a word????
    I Speak in frequencies even dogs have trouble hearing


  6. #6
    Banned
    Join Date
    Apr 2003
    Posts
    51
    one loads a word from memory the other stores...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides