如何通过指针将模板成员函数传递给另一个成员函数

How would I pass a template member function by pointer to another member function?

本文关键字:成员 函数 另一个 何通过 指针      更新时间:2023-10-16

我希望能够传入小部件类型及其操作的映射,以及对下面示例中显示的模板成员函数的引用,这样我就能够在一个简单的数据驱动循环中处理绑定。我已经做了一些尝试,但不知道如何在没有参数的情况下将模板函数作为函数指针传递。

(我也愿意接受其他关于如何在c++中实现这一点的建议(

void AVRPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) {
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAction(LP_Constant::TRIGGER_PRESS_RIGHT_ACTION, IE_Pressed, this, &AVRPawn::TriggerPressed<IE_Pressed, ESide::Right>);
PlayerInputComponent->BindAction(LP_Constant::TRIGGER_PRESS_RIGHT_ACTION, IE_Released, this, &AVRPawn::TriggerPressed<IE_Released, ESide::Right>);
PlayerInputComponent->BindAction(LP_Constant::TRIGGER_PRESS_LEFT_ACTION, IE_Pressed, this, &AVRPawn::TriggerPressed<IE_Pressed, ESide::Left>);
PlayerInputComponent->BindAction(LP_Constant::TRIGGER_PRESS_LEFT_ACTION, IE_Released, this, &AVRPawn::TriggerPressed<IE_Released, ESide::Left>);
}

在javascript中,我会创建一个简单的高阶函数来封装不同的数据,但我很难在c++中找到一种有效的方法。

所以我的解决方案最终是非基于模板的。这是UE4框架的限制,或者更准确地说,是我对它缺乏深入的了解。我最终找到了一种绑定lambda回调的方法,我将Action和Event传递给它。

void AVRPawn::BindPressableInput(UInputComponent* InputComponent) {
for (auto const& Controller : MotionControllers) {
for (auto const& Action : Controller->GetInputActions()) {
for (auto const& Event : PressAndReleaseEvents) {
FInputActionBinding Binding(Action, Event);
Binding.ActionDelegate.GetDelegateForManualSet().BindLambda([=, &Controller]() {
Controller->HandleInputEvent(Action, Event);
});
InputComponent->AddActionBinding(Binding);
}
}
}
}
void AVRPawn::SetupPlayerInputComponent(UInputComponent* InputComponent) {
Super::SetupPlayerInputComponent(InputComponent);
BindPressableInput(InputComponent);
}