CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    Dec 2003
    Location
    St. Cugat - Catalunya
    Posts
    441

    Resolved Can a function call be stored in a variable?

    Let's see if I can explain what I would like to do (sorry for the inacuracy, I'm not a C expert).

    I have a set of (similar) functions called depending on some value. Kind of (simplified):

    Code:
    int Func_0(int i)
    {
    ...
        return j;
    }
    int Func_1(int i)
    {
    ...
        return j;
    }
    ...
    int Func_n(int i)
    {
    ...
        return j;
    }
    
    ...
    for (i=0; i<max_func; ++i)
    {
        switch (i)
        {
           case 0:
                retval=Func_0(k);
                break;
           case 1:
                retval=Func_1(k);
                break;
           ...
           case n:
                retval=Func_n(k);
                break;
        }
        // use retval on some code
        ...
    }
    The real situation is more complex obviously, but I hope it's clear enough

    Now, I would like to do something like ... (no laughing please! )
    Code:
    FuncType MyFuncs[max_func];                    // declare an array of "functions".
    MyFuncs[0]=AddressOf(Func_0);     // store the function calls
    MyFuncs[1]=AddressOf(Func_1);
    ...
    MyFuncs[n]=AddressOf(Func_n);
    ...
    
    // now use them
    
    for (i=0; i<max_func; ++i)
    {
        retval=MyFuncs[0](k);
        // use retval
       ...
    }
    I know most of it is not valid syntax, but I hope the idea is clear.

    Is this possible?

    Thks
    Last edited by DeepButi; November 11th, 2008 at 04:34 PM.
    Did it help? rate it.

    The best conversation I had was over forty million years ago ... and that was with a coffee machine.

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