I'm developing an app, in which I have a lot of small algorithms, each algorithm represented by some lines of code, so I want to store that few lines of code as functions, but not only that, I have to store some data that each algorithm has, so I decided to make a "Algorithm" class, in which, I would have in a "variable" the function stored. So, I can use it later.
I don't know if this is possible or if there is another way to reach that. I think there would be problems with local variables or private members of the class where my "algorithms" are.
class Patterns {
private:
double line;
double addPoint(char n) {line += n;}
public:
double addPattern(int m) {
double tmp = 0;
char param;
// some calculations with m
// many calls to addPoint, "algorithm"
tmp += addPoint(param); // param1
tmp += addPoint(param); // param2
tmp += addPoint(param); // param3
tmp += addPoint(param); // param4
return tmp;
}
}
just a little sample, I want to store all those lines of "addPoints()" in only one function, and use whenever I want, something like this
class Patterns {
private:
double line;
double addPoint(char n) {line += n;}
public:
double addPattern(int m) {
double tmp = 0;
// some calculations with m
/**
* vector of Algorithm class, get any Algorithm, and of that,
* return the stored function, so I can use it as in the above sample
*/
auto fn = vec->back()->getFunction();
tmp += fn(m)
return tmp;
}
}
Edit: this question includes the use of the library <functional>