PDA

Click to See Complete Forum and Search --> : C++ function address to as a parameter


Q-bel
February 25th, 2004, 07:40 PM
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.

slarty
February 25th, 2004, 08:00 PM
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:


// 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);
}

SexyBadGirl
February 25th, 2004, 08:32 PM
He wants to pass a functions address (pointer) to another function as a parameter. And yes it's possible.

Q-bel
February 28th, 2004, 06:12 PM
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

Q-bel
February 28th, 2004, 09:47 PM
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????

amnesiac
March 8th, 2004, 11:07 PM
one loads a word from memory the other stores...