I need to maintain a list of methods that will be executed in different orders for testing. We are moving away from C to C++ to use google framework. Is it possible to maintain a list of functions pointers to some of the class methods to be used for execution inside the class, so that they can be used after instantiation ? please refer http://cpp.sh/265y
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef void (*funcType)();
class Sample {
public:
vector<funcType> func_list;
Sample();
void formList();
void method1();
void method2();
void method3();
};
void Sample::formList() {
func_list.push_back(&method1);
func_list.push_back(&method2);
func_list.push_back(&method3);
}
void Sample::method1 () {
cout << "method1" << endl;
}
void Sample::method2 () {
cout << "method2" << endl;
}
void Sample::method3 () {
cout << "method3" << endl;
}
int main()
{
Sample sample; //* = new Sample();
sample.formList();
vector<funcType>::iterator it;
for (it = sample.func_list.begin(); it != sample.func_list.end(); ++it) {
((*it));
}
}
Answer: http://cpp.sh/8rr2