在类变量中的类方法的列表/向量 [英] list/vector of class methods in a class variable
问题描述
我需要维护一个将以不同顺序执行的方法列表以进行测试。我们正在从C到C ++使用google框架。是否可以维护一个函数列表,指向类中的一些类方法的指针,以便在实例化之后使用它们?请参阅 http://cpp.sh/265y
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));
}
}
推荐答案
您可以使用可怕的指针到成员函数语法:
You could use the dreaded pointer-to-member-function syntax:
// Example program
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Sample;
typedef void (Sample::*funcType)();
class Sample {
public:
vector<funcType> func_list;
Sample(){}
void formList();
void method1();
void method2();
void method3();
};
void Sample::formList() {
func_list.push_back(&Sample::method1);
func_list.push_back(&Sample::method2);
func_list.push_back(&Sample::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) {
(sample.*(*it))(); // HORRIFIC, INNIT? SEE BELOW FOR BETTER
}
}
更好:
但是,你可以使用C ++ 11 / TR1 std :: function<>
或 boost :: function<>
:
MUCH BETTER:
However, you could be much more versatile using C++11/TR1 std::function<>
or boost::function<>
:
typedef function<void(Sample*)> funcType;
// ...
func_list.push_back(mem_fn(&Sample::method1));
func_list.push_back(mem_fn(&Sample::method2));
func_list.push_back(mem_fn(&Sample::method3));
查看 Live on Coliru 也
for (it = sample.func_list.begin(); it != sample.func_list.end(); ++it) {
(*it)(&sample); // much better
}
因为您可以满足不同的签名: Live On Coliru
class Sample {
public:
vector<funcType> func_list;
Sample(){}
void formList();
void method1(int);
void method2(std::string);
void method3(double, double, double);
};
void Sample::formList() {
using std::placeholders::_1;
func_list.push_back(bind(&Sample::method1, _1, 42));
func_list.push_back(bind(&Sample::method2, _1, "Hello world"));
func_list.push_back(bind(&Sample::method3, _1, 1, 2, 3));
}
这篇关于在类变量中的类方法的列表/向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!