class 变量中的 list/vector 个 class 方法
list/vector of class methods in a class variable
我需要维护一个方法列表,这些方法将以不同的顺序执行以进行测试。我们正在从 C 转向 C++ 以使用 google 框架。是否可以维护一个指向一些 class 方法的函数指针列表以用于在 class 中执行,以便它们可以在实例化后使用?请参考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));
}
}
简介
您当前已将 funcType
声明为 void(*)()
的别名,它是指向某个不带参数且 returns 无效的函数的指针。您应该使用的是指向成员函数的指针,因为这将适合您尝试调用的实体。
class Sample;
typedef void (Sample::*funcType)();
你还必须限定你的成员函数,当你要获取他们的地址时:
void Sample::formList() {
func_list.push_back(&Sample::method1);
func_list.push_back(&Sample::method2);
func_list.push_back(&Sample::method3);
}
当调用一个成员函数时,你需要一个你想在其上调用它的对象,这意味着你——为了通过指向成员的指针调用一个成员函数-function—必须在调用点提供一个对象。
for (it = sample.func_list.begin(); it != sample.func_list.end(); ++it) {
(sample.*(*it)) (); // invoke the member-function referred to by `it`
} // on the object named `sample`
进一步阅读
- whosebug.com - Calling member-function pointer
- cppreference.com - Pointers to member functions
实施示例
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Sample;
typedef void (Sample::*funcType)();
class Sample {
public:
vector<funcType> func_list;
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)) ();
}
}
method1
method2
method3
您可以使用可怕的成员函数指针语法:
// 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<>
:
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
}
The added versatility is mainly in that you can cater for different signatures: 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));
}
我需要维护一个方法列表,这些方法将以不同的顺序执行以进行测试。我们正在从 C 转向 C++ 以使用 google 框架。是否可以维护一个指向一些 class 方法的函数指针列表以用于在 class 中执行,以便它们可以在实例化后使用?请参考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));
}
}
简介
您当前已将 funcType
声明为 void(*)()
的别名,它是指向某个不带参数且 returns 无效的函数的指针。您应该使用的是指向成员函数的指针,因为这将适合您尝试调用的实体。
class Sample;
typedef void (Sample::*funcType)();
你还必须限定你的成员函数,当你要获取他们的地址时:
void Sample::formList() {
func_list.push_back(&Sample::method1);
func_list.push_back(&Sample::method2);
func_list.push_back(&Sample::method3);
}
当调用一个成员函数时,你需要一个你想在其上调用它的对象,这意味着你——为了通过指向成员的指针调用一个成员函数-function—必须在调用点提供一个对象。
for (it = sample.func_list.begin(); it != sample.func_list.end(); ++it) {
(sample.*(*it)) (); // invoke the member-function referred to by `it`
} // on the object named `sample`
进一步阅读
- whosebug.com - Calling member-function pointer
- cppreference.com - Pointers to member functions
实施示例
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Sample;
typedef void (Sample::*funcType)();
class Sample {
public:
vector<funcType> func_list;
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)) ();
}
}
method1
method2
method3
您可以使用可怕的成员函数指针语法:
// 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<>
:
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
}
The added versatility is mainly in that you can cater for different signatures: 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)); }