CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2002
    Location
    New Delhi
    Posts
    25

    C/C++ Function Call

    I wonder if it is possible to call a function whose name is stored in a character variable.
    i.e. if Str1[4] holds "Sum" Then is it possible to call Sum by refering it through Str1? Inline Assembly, Macro Substitution, parameterized classes or any **** solution is most welcome.

    Spread Love and Knowledge.
    Spread Love And Knowledge

  2. #2
    Join Date
    Apr 2000
    Location
    Los Angeles, California, USA
    Posts
    28

    Re: C/C++ Function Call


    typedef void (*MyFuncPtr) (void);
    typedef struct __vTableEntry
    {
    const char* pszFunctionName;
    MyFuncPtr fptr;

    } VTENTRY;

    const VTENTRY g_VTable[] =
    {
    {"Diff", Diff},
    {"Sum", Sum},
    ...
    };

    void execute (const char *pszFunctionName)
    {
    // search for the vtable entry, witht the specified function name
    VTENTRY* pEntry = search_vtable (g_VTable, pszFunctionName);

    if (pEntry != NULL) // if the entry was found
    pEntry->fptr (); // execute function
    }




    You can presort your vtable array and use _bsearch to find the function
    pointer, associated with the string or use a map:


    #include <map>
    #include <string>


    typedef void (*MyFuncPtr) (void);
    typedef std::map <std::string, MyFuncPtr> MyVTableType;

    MyVTableType g_VTable;

    void main (void)
    {
    // preallocate vtable
    g_VTable["Diff"] = Diff;
    g_VTable["Sum"] = Sum;

    ...
    // Execute function
    MyVTableType::iterator i_fptr = g_VTable.find ("Sum");
    if (i_fptr != g_VTable.end())
    (*i_fptr)();
    }



    P.S.
    In my examples I was assuming that the functions, stored in the vTable have the same
    signatures: return the same type and use the same arguments. If this is not the case,
    you can pass va_list for the arguments and use template to handle different return values.



  3. #3
    Join Date
    Oct 2001
    Location
    Chicago, USA
    Posts
    222

    Re: C/C++ Function Call

    why and where would you use this? dont people use array of function pointers for similar calling a desired function(never used them myself, just heard)? i know it is a little different, but i want to know why would you want to call a function stored in a string?

    a little explaination would be awesome..

    thanx
    Ankur


  4. #4
    Join Date
    Feb 2002
    Location
    New Delhi
    Posts
    25

    Re: C/C++ Function Call

    It's about really making things flexible. U See if this were possible the way i want, I would be able to define the rules (applied to the incomming data) into the ini file and thus making things quite generalized. The Scene is - Data comming from switches (telecom) and the flags often modify the meaning of the field. And on top of it the modification rules are just different for different packages.

    Spread Love and Knowledge.
    Spread Love And Knowledge

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: C/C++ Function Call

    mtrosman gave you two possible solutions, of which his second (using std::map) is the best approach. His actual code was for illustration purpose. Your job is to adapt the idea for your specific requirements.

    If that is not what you are looking for, I suggest you give a clearer specification.


    The best things come to those who rate

  6. #6
    Join Date
    Feb 2002
    Location
    New Delhi
    Posts
    25

    Re: C/C++ Function Call

    Your solution really helped me thinking in new direction. But may be i wasn't very clear in putting up my requirements. I needed something like an & operator in Foxpro or a function like "CallByName" in Visual Basic. The idea of mapping is not what i required. Thanks really.

    Spread Love and Knowledge.
    Spread Love And Knowledge

  7. #7
    Join Date
    Feb 2002
    Location
    New Delhi
    Posts
    25

    Re: C/C++ Function Call

    I wish to read an ini file whereupon i wish to write something like this.
    ValueModifier = "Sum F1 F2"
    When i have the above string "Sum F1 F2" read in some character array, i should be able to call 'Sum' passing F1 F2 as parameters to Sum.
    mtrosman solution will surely fit into this requirement but i was just hoping that there is some way similer to & of Foxpro or CallbyName in visual Basic........I wanted to be sure that there is no such possiblity in c/c++. mtrosman's solution has given me an idea of changing my requirements to suit this implementation of mapping function nams with the corresponding function pointers.

    Spread Love and Knowledge.
    Spread Love And Knowledge

  8. #8
    Join Date
    Mar 2002
    Location
    Israel
    Posts
    187

    Re: C/C++ Function Call

    Hi Neerad,

    templates, macro, function names etc. are compilation stage instruments.
    Your wish is to interpret .ini file at run time.
    So, whatever You do, it should be probably association SymbolicInfo-->FunctionPointer.
    There are a couple of solutions to do this job.
    In addition to using std::map You can use Command design pattern and RTTI mechanism
    to determine witch Command-derived object to build.
    Better variant of this solution is to use Builder class.
    Another solution - to put Sum, Sub etc. functions into dll and use
    its export table to associate symbolic name with function pointer. (Microsoft specific solution



    In the future computer's weight will not exceed 1.5 ton.
    (Popular Mechanics, 1949)

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

Featured