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()
{
unsignedint numLayers = 10000;
for(unsignedint 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.
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(unsignedint 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.
class A
{
public:
A();
void callThem(unsignedint index);
......
......
/*
Functions assigned to the array are declared and defined
*/
private:
typedefvoid (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(unsignedint 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];
}
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
usingnamespace std;
class A
{
private:
typedefvoid (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(unsignedint 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]();
^