3

I can't seem to find any relevant information on the following sort of thing.

Say that you have a program with numerous methods (for example, a custom set of tests).

How could you loop through them based on something like the following pseudo-code

for(int i= 0;  i < 10 ; i ++)
    {
        function(i)();

    }

so that it will go through this loop and therefore launch methods function0, function1, function2, function3, function4, function5, function6, function7, functuin8, function9.

If there are ways to also do this in C# or Java, then information for them also would be appreciated.

2
  • Are all the functions parameter-less? What about the return type? Commented Nov 15, 2013 at 21:30
  • In general. Not specific to return types and such. Commented Nov 15, 2013 at 21:43

4 Answers 4

2

In C++, the only way I can think of is to use of an array of function pointers. See here. For Java, which supports Reflection, see this. And for C#, which also supports Reflection, this.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the multiple references, along with answering the question for all three languages.
2

The language feature you would need for this is called "Reflection", which is a feature C++ does not have. You will need to explicitly name the functions you want to call.

Comments

1

Well, if you have an array of function pointers, you can do something like this:

void (*myStuff[256])(void);

And then when you want to call each function just dereference each of them as you iterate.

Keep in mind that every function in your array must have the same parameter signature and return type.

1 Comment

You could use something like Boost.Bind or std::bind to transform your functions such that they all have the same signatures. I have used this very technique in the past to build arbitrary sequences of functions (with differing signatures) which I can execute in one go from a single functor.
1

Here's a solution using Boost.Function and Boost.Bind in which the loop doesn't need to worry about the parameter signatures of the functions you are calling (I haven't tested it in a compiler, but I have very similar code in a project which I know works):

#include <vector>
#include <boost/function.hpp>
#include <boost/bind.hpp>

using std::vector;
using boost::function;
using boost::bind;

void foo (int a);
void bar (double a);
void baz (int a, double b);

int main()
{
   // Transform the functions so that they all have the same signature,
   // (with pre-determined arguments), and add them to a vector:
   vector<function<void()>> myFunctions;
   myFunctions.push_back(bind(&foo, 1));
   myFunctions.push_back(bind(&bar, 2.0));
   myFunctions.push_back(bind(&baz, 1, 2.0));

   // Call the functions in a loop:
   vector<function<void()>>::iterator it = myFunctions.begin();
   while (it != myFunctions.end())
   {
       (*it)();
      it++;
   }

   return 0;
}

Note that you can do the loop much easier if your compiler supports C++11:

   // Call the functions in a loop:
   for (const auto& f : myFunctions)
   {
      f();
   }

Boost.Bind also supports passing in certain parameters dynamically instead of binding them to pre-determined values. See the documentation for more details. You could also trivially alter the above code to support return values (if they are of the same type), by replacing void with the return type, and altering the loop to do something with the returned value.

1 Comment

It's worth noting, that with C++11 supported compilers (pretty much all of them these days) that you can do exactly the same thing with the <functional> header. It has std::function and std::bind which use the same syntax as what you're doing. So you don't need boost for it anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.