gpt4 book ai didi

c++ 函数队列

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:16:16 26 4
gpt4 key购买 nike

所以我有多个带有不同参数的方法:

class c;

void c::foo1(int a) {

}

void c::foo2(int a, int b) {

}

如何获得这些函数的队列/vector ?它不一定必须是 std::function 对象,但我需要一种方法来对函数的执行进行排队。

最佳答案

您的问题有点含糊,但如果您在将函数插入队列时已经知道目标对象和调用的参数,则可以使用无参数 lambda 队列:

std::deque<std::function<void()>> q;
c x;
q.push_back( [&x] { x.foo1(1); } );
q.push_back( [&x] { x.foo2(2,3); } );
// ... some time later we might want to execute the functions in the queue ...
while (!q.empty())
{
q.front()();
q.pop_front();
}

关于c++ 函数队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22109555/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com