Results 1 to 8 of 8

Thread: I hope it is possilble

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    I hope it is possilble

    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:

    PHP Code:
    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.
    Baaaaaaaaah

  2. #2
    You mean like VB's evil CallByName? I don't think you can in C++.

  3. #3

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Does vb have something like that?Hmm...I want the similiar thing in C++ because it will make coding a lot simpler and smaller.
    Baaaaaaaaah

  4. #4
    ...and much more prone to error. I loathe CallByName.

  5. #5

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    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
    Baaaaaaaaah

  6. #6
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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:
    Code:
    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.
    Code:
    // 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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    And a way cool thing as well, you can call a buffer as a function
    Code:
    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).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

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



Click Here to Expand Forum to Full Width