Click to See Complete Forum and Search --> : I hope it is possilble
abdul
Sep 24th, 2001, 05:57 PM
I will be writting a graphics library (EG: button, scrollbar,etc..) shortly but I just want to know one thing:
How do I pass a function to a function so that the function executes the passed function by its name.
Here is a little of what I am talking about:
void ExecuteFunction(char *whichfunction)
{
/*Execute the function passed as the perameter but it won't
work because the variable is of a char type and I also can't write
the variable value and let it execute as a function*/
whichfunction
}
I hope you got a clue of what I am talking about from the code above.
filburt1
Sep 24th, 2001, 06:11 PM
You mean like VB's evil CallByName? I don't think you can in C++.
abdul
Sep 24th, 2001, 06:26 PM
Does vb have something like that?Hmm...I want the similiar thing in C++ because it will make coding a lot simpler and smaller.
filburt1
Sep 24th, 2001, 06:59 PM
...and much more prone to error. I loathe CallByName. :)
abdul
Sep 24th, 2001, 08:07 PM
I see...
I actually had the idea of a button class so that it runs its own loop and check for the mouse position. If the button is clicked (still within), I run the function that passed to the button class in some other variable.
Otherwise I will have to catch the mouse event for the button myself:(
crptcblade
Sep 24th, 2001, 09:20 PM
Originally posted by filburt1
You mean like VB's evil CallByName? I don't think you can in C++.
There is nothing you can do in VB that can't be done in C++.
I don't know how you would do it, but I can see an example in the way you setup a window class. When you pass the name of the WndProc you are going to associate with the window.
:)
CornedBee
Sep 25th, 2001, 09:28 AM
I think this is about the only thing that can be done in VB that cannot be done in C++. This is because the compiler needs to know at compile time what to do. A function call is compiled to something like this:
CALL address of function
But a string is in no way related to a function. You could do something like this:
void CallFunc(char* funcname)
{
if(!strcmp(funcname, "FirstFunc"))
{
FirstFunc();
}
else if(!strcmp(funcname, "SecondFunc"))
{
SecondFunc();
}
// and and and...
}
Actually this would be the thing CallByName does (or something similar). But this works only for preknown function names, so the VB compiler has to parse each function name and prepare CallByName for it - because it can't know which function is going to be called later.
But in C there are function pointers. Looking at the asm code above you might ask yourself if the address must be a real function (it's address is inserted by the linker). It could be a value of a pointer just as well because this would be an adress too. It is possible. You can assign a pointer the addresss of a function and later call the functin through the pointer.
// the function pointer must know which type of function it points to
// for example the WNDPROC pointer is defined like this:
typedef LRESULT (CALLBACK* WNDPROC)(HWND, UINT, WPARAM, LPARAM);
// the brackets around the function name are necessary.
// Function pointers don't need the typedefs, so this could happen:
// global function pointer or function?
int *FuncPtr(int);
// This is interpreted as a function named FuncPtr with int* return type
// this is a pointer named FuncPtr to functions with int return type:
int (*FuncPtr)(int);
// This is a little bit complicated, this is why there are typedefs like above
// Now it's easy to declare a pointer to a WNDPROC
WNDPROC pfnWndProc;
// such a pointer is a member of the WNDCLASS and WNDCLASSEX structs
// to assign a value to a function pointer, use the function name without a parameter list.
pfnWndProc = MyWndProc;
If every object of your library had a 32-bit value assigned to it, it could store the function address of the function to call when something happens. You could then call the referenced function when required. This qould require that all functions have the same parameters and return types, but even this can be avoided. Study the MFC message routing mechanism to learn how. (But I warn ya, this is hard!)
Actually, by storing many such pointers in a C-struct, you could nearly create a C++-class. by adding a lot of macros and some programmer responsibility, you would only lack operator and type cast overloading, real class deriving (but you could create an illusion), automatic constructor and destructor calling (could also be simulated by macros) and an automatic this-pointer (yould also be simulated). If you want I could take some time to show you how.
parksie
Sep 25th, 2001, 11:20 AM
And a way cool thing as well, you can call a buffer as a function :)char *buffer = get_function_code_from_somewhere();
typedef long (*FUNCPTR)(int, int);
long compatible(int x, int y) {
/* This function is compatible with the function pointer */
return x + y;
}
void somecode() {
FUNCPTR pfn = (FUNCPTR)buffer;
(*pfn)(6, 7);
}Just make sure your function has REAL instructions in it.
This is one way to make self-modifying code (modify the machine code in memory, and then write it back to the .exe file).
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.