a cycle for a number of times

Hello forum,

I have four different functions as follows:

1
2
3
4
   void func1();
   void func2();
   void func3();
   void func4();


And each of the above declared function will be called in the sequential cycle pattern inside a for loop for an assigned number of times as follows:

 
func1() func2() func3() func4() func1() func2() func3() func4() .........


Some hint on the algorithm formulation will be helpful.


Thanks
make an array of function pointers then.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void func1();
   void func2();
   void func3();
   void func4();

const int N_STAGES = 4;
void (*Pipeline[N_STAGES ]) (void);


  Pipeline[0] = func1;
  Pipeline[1] = func2;
  Pipeline[2] = func3;
  Pipeline[3] = func4;

int currentStage = 0;
for( ; currentStage < N_STAGES ; currentStage ++)
      Pipeline[currentStage]();

Was that what you wanted ?
Let me try again , I believe I have not explained the issue well enough last time. Check the following snippet :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
void scanAlongPositiveX()
{
    std::cout << "Scanning along the positive X-axis" << std::endl;
}

void scanAlongNegativeX()
{
    std::cout << "Scanning along the negative X-axis" << std::endl;
}

void scanAlongPositiveY()
{
    std::cout << "Scanning along the positive Y-axis" << std::endl;
}


void scanAlongNegativeY()
{
    std::cout << "Scanning along the negative Y-axis" << std::endl;
}


int main()
{    

    unsigned int numLayers = 10000;

    for(unsigned int i = 0; i < numLayers; ++i)
    {
         /*
               the value of i will choose the function call and calling pattern will be as follows:

               void scanAlongPositiveX();
               void scanAlongNegativeX();
               void scanAlongPositiveY();
               void scanAlongNegativeY();

               void scanAlongPositiveX();
               void scanAlongNegativeX();
               void scanAlongPositiveY();
               void scanAlongNegativeY();

                            .
                            .
                            .
                            .
                            .
               void scanAlongPositiveX();
               void scanAlongNegativeX();
               void scanAlongPositiveY();
               void scanAlongNegativeY();           
         */
    }
    
    return 0;
}


I hope that you can see the pattern now. It obligatory that index i inside the for loop will choose the function to be called. I can imagine some modulas operator along with function pointer or function object, but yet to formulate the algorithm.

Looking forward to some more feed-back.


Thanks
Ericool approach is good for this.

I can imagine some modulas operator along with function pointer or function object
Exactly what you need

1
2
3
4
5
6
7
8
using funct_t = void(*)();
const size_t func_num = 4;
func_t functions[func_num] = {
    scanAlongPositiveX, scanAlongNegativeX, 
    scanAlongPositiveY, scanAlongNegativeY
};
for(unsigned int i = 0; i < numLayers; ++i)
    functions[i % func_num](); //Call function depending on value of i 
If you do not want an array of function pointers, you can stick a switch there instead.
Last edited on
Thanks for the hint!

I am having compilation issues while incorporating class structure into this. So I had to reformulate it as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

class A
{
   public:
       A();
       void callThem(unsigned int index);
       ......
       ......
       /*
            Functions assigned to the array are declared and defined
        */
   private:

    typedef void (A::*MemFunc)();

    /**
	* \brief Declare array of function pointers
	**/
    MemFunc a[4];   
};


A::A()
{
	a[0] = &A::createSegmentsAlongPositiveX;
	a[1] = &A::createSegmentsAlongNegativeX;
	a[2] = &A::createSegmentsAlongPositiveY;
	a[3] = &A::createSegmentsAlongNegativeY;
}

void A::callThem(unsigned int index)
{
     /*
        Here is the confusion. If I add parenthesis like a[index % 4](), I get compilation error 
        mentioning that 
        error C2064: term does not evaluate to a function taking 0 arguments

        Will those function be really called ? It compiles fine if I remove the parenthesis
        
     */
     a[index % 4];
}



Thanks
You have an array of member function pointers. They are not usual functions. They should be called on some object.

If your member fuctions do not really need to operate on object instance, make them static and use them like array of normal (non-member) functions.

Otherwise you should use member function pointer call.
This will cal them on current object:

this->*a[index % 4]();
Thanks !

Getting compilation error with the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;



class A
{
private:
    typedef void (A::*MemFunc)();

    MemFunc a[4];
public:

    A()
    {
        a[0] = &A::scanAlongPositiveX;
        a[1] = &A::scanAlongNegativeX;
        a[2] = &A::scanAlongPositiveY;
        a[3] = &A::scanAlongNegativeY;
    }

    //some functional pattern
    void scanAlongPositiveX()
    {
        std::cout << "Scanning along the positive X-axis" << std::endl;
    }

    void scanAlongNegativeX()
    {
        std::cout << "Scanning along the negative X-axis" << std::endl;
    }

    void scanAlongPositiveY()
    {
        std::cout << "Scanning along the positive Y-axis" << std::endl;
    }


    void scanAlongNegativeY()
    {
        std::cout << "Scanning along the negative Y-axis" << std::endl;
    }

    void callOnThem();
};


void A::callOnThem()
{
    for(unsigned int i = 0; i < 100; ++i)
    {
        this->*a[i%4]();
    }
}

int main()
{    

    A a;

    a.callOnThem();

    return 0;
}


The compilation error that I am getting is as follows:

1
2
3
/home/sajjad/Documents/Wematter/TestBed/ParallelScanPattern/main.cpp:56: error: must use '.*' or '->*' to call pointer-to-member function in '((A*)this)->A::a[(i & 3u)] (...)', e.g. '(... ->* ((A*)this)->A::a[(i & 3u)]) (...)'
         this->*a[i%4]();
                       ^


Any more hint ?
Precedence: (this->*a[i%4])();
Topic archived. No new replies allowed.