Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through multiple functions in C++?

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.

like image 391
Sbspider Avatar asked Jun 10 '26 14:06

Sbspider


2 Answers

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.

like image 82
neutrino Avatar answered Jun 13 '26 04:06

neutrino


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.

like image 34
Billy ONeal Avatar answered Jun 13 '26 03:06

Billy ONeal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!