Is it possible to create a vector that has functions pushed back?
I've tried doing something with pointers, but it only works with functions without parameters.
For example,
#include <iostream>
#include <vector>
using namespace std;
void printInt();
int main()
{
vector<void (*)()> functionStack;
functionStack.push_back(printInt);
(*functionStack[0])();
}
void printInt()
{
cout << "function works!" << 123 << endl;
}
That works, but not what I need.
The correct version of that would be a function that has parameters: void printInt(int a)
and you could call it with different values like 4
or -1
but from the vector functionStack
.
It's probably more complex if the functions in the vector are with different parameters, so let's assume that every function has the same type and amount of parameters.
vector<void (*)(int)> functionStack;
and, correspondingly, elsewhere,void printInt(int a);
?printStr
in the question is just a typo I assume?