-
February 25th, 2004, 07:40 PM
#1
Member
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
-
February 25th, 2004, 08:00 PM
#2
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);
}
-
February 25th, 2004, 08:32 PM
#3
Banned
He wants to pass a functions address (pointer) to another function as a parameter. And yes it's possible.
-
February 28th, 2004, 06:12 PM
#4
Member
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
-
February 28th, 2004, 09:47 PM
#5
Member
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
-
March 8th, 2004, 11:07 PM
#6
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
-
Forum Rules
|
|