Results 1 to 9 of 9

Thread: array of functions

  1. #1

    Thread Starter
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking array of functions

    Hello, just wondering how to have an array of functions.

    You see, i have at the moment:
    Code:
    string ret;
    switch (some_var)
    case 1:
       ret = some_function1(true); break;
    case 2:
       ret = some_function2(false); break;
    ....
    but it would be nicer to make it:
    Code:
    ret = func_array[some_var](bool_input[some_var]);
    now, the bool_input array is easy, just have = { true, false...}
    but how do i get the array of funcitons to work?
    sql_lall

  2. #2
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    You can make an array of function pointers:
    Code:
    string func1(bool);
    string func2(bool);
    //...
    
    typedef string (*bool_to_string_func)(bool);
    bool_to_string_func func_array[] = {func1,func2};

  3. #3

    Thread Starter
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking ok

    thanks.
    I guess there's no way to have such an array with functions that take various parameters. (i.e. some take one bool, other take two)
    However, thanks for the code.
    I guess to get different types of input, i can have the parameter of a vector<bool>, that way i can input as many as necessary...
    sql_lall

  4. #4
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Sure there is.

    Just make all of the functions that require fewer args use varargs - the ones that do not need all two or three parameters.

  5. #5

    Thread Starter
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking umm...well

    Yeah, unfortunately it didn't quite work.
    How do you then call the funcitons?

    func_array[1](true, true)??

    and is there any way to use only one line, without the typedef?


    Oh, one other small problem. I made a simple template function, outputs a value to file. This was a function in a class, and when i put the function in the class definition, it worked fine.
    However, i prefer putting class function code in a .cpp file, so i tried putting in there:
    template <class T> void myclass:rint(T input)
    {}

    but this wouldn't work. Any suggestions?

    Note that in my class i had template <class T> print(T) - is this the correct prototype, or have i missed something??

    Thanks
    sql_lall

  6. #6
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    template functions must always be put in header files (*)

    And a vector<bool> or maybe a bool[] would be a better choice then a vararg function. Varargs are quite weird.

    (*) you could use the export keyword or explicit instanciation, but you probably don't want to!

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The export keyword is a non-standard extension. Supported AFAIK by VC++ and gcc, but not sure about that.
    Explicit instantiation is useful only in some cases.

    template <class T> void myclass::print(T input)
    should be
    template <class T> void myclass<T>::print(T input)
    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
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    For variable arguments, make each function take a
    std::vector<boost::any>
    .
    This requires you to get the boost library from www.boost.org .
    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.

  9. #9

    Thread Starter
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking right...

    Ok, thanks for that.
    I think i'll just leave it in the .h file. It was only a small class, used for debugging stuff, so if i only have to add one file not two for each project i guess it helps anyway.

    However, i still can't get the function thing to work.
    I even tried a simple case (a single pointer, rather than array, to an int function that takes an int parameter) but still this didn't work.
    I'll probably post the code i'm using soon, so you can see exactly whats happening. However if it is possible i'll try every ordering of syntax, see if one is right
    sql_lall

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