如何用不同的参数类型和数字调用函数

问题描述 投票:0回答:1

我在类中声明了以下类型:

using ScriptFunction = std::function<void(std::string const&)>;
using Script_map = std::unordered_map<std::string, std::vector<ScriptFunction>>;

在这个类中,我希望存储其他类中的一些成员函数,并像这样调用它:

void Manager::callback(std::string event, std::string data) {
    auto it = this->stored_func.find(event);                     //stored_func is a Script_map
    for (auto func : it->second) {
        func(data);
    }
}

该代码有效,但是,我想更改它,以便可以回调接收1个或2个字符串或根本没有参数的函数。这可能吗?

c++ callback variadic-templates
1个回答
0
投票

可能对您有用的是带有可变参数的方法。与此类似。

Variable number of arguments in C++?

© www.soinside.com 2019 - 2024. All rights reserved.