如何将成员函数作为参数传递并在派生对象上执行方法列表



我想创建一个将整数值映射到成员函数的数组,以便

(this->*actionMap[i])();

执行该方法。 为了填充数组,我想要一个将数组元素设置为相应操作的方法。

我在之前的问题中看到它应该使用 std::function 和 std::bind 但我没有遵循语法,而且我不明白如何声明数组: 如何正确地将成员函数作为参数传递

这是M(非(我们 请注意,我希望基类能够在派生对象上执行方法。

#include <iostream>
using namespace std;
class Base;
typedef void (Base::*Action)();
class Base {
Action actions[3];
public:
void setAction(int a, Action act) {
actions[a] = act;
}
void f() { cout << "f"; }
void go() {
for (int i = 0;  i < 3; i++)
(this->*actions[i])();
}
};
struct Derived : public Base {
void g() { cout << "g"; }
void h() { cout << "h"; }
Derived() {
setAction(1, f);
setAction(2, g);
setAction(1, h);
}
};
int main() {
Derived d;
d.go();
}

派生对象上执行方法。

因此,在执行方法时,必须具有派生对象的句柄。其中两种方法不在Base::,因为它们不在Base内部。它们在Derived内部,所以指针可能是Derived::*的,但这毫无意义,并且会破坏我猜你想要的模型。我想你可以让你的方法ghfBase内部的虚拟。但这又会破坏目的,我猜是一种类似观察者的模式。

你想做的,基本上很容易通过适当的抽象来解决 -std::functionstd::bind

#include <iostream>
#include <array>
#include <functional>
class Base {
std::array<std::function<void()>, 3> actions;
public:
void setAction(int a, std::function<void()> act) {
actions.at(a) = act;
}
void f() { std::cout << "f"; }
void go() {
for (auto&& action : actions) {
if (action) {
action();
}  
}
}
};
struct Derived : public Base {
void g() { std::cout << "g"; }
void h() { std::cout << "h"; }
Derived() {
setAction(1, std::bind(&Derived::f, this));
setAction(2, std::bind(&Derived::g, this));
setAction(1, std::bind(&Derived::h, this));
}
};
int main() {
Derived d;
d.go();
}

最新更新