Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 带或不带参数的动态函数调用及其在类中的存储_C++_C++11 - Fatal编程技术网

C++ 带或不带参数的动态函数调用及其在类中的存储

C++ 带或不带参数的动态函数调用及其在类中的存储,c++,c++11,C++,C++11,我使用c++11,不可能使用QTimer,因为我的程序不是图形化的 我不明白如何存储指针函数及其参数,并在run方法中调用此函数 谢谢你的帮助 计时器.h: #ifndef TIMER_H #define TIMER_H #include <atomic> #include <thread> #include <chrono> #include <iostream> #include <list> using namespace s

我使用c++11,不可能使用QTimer,因为我的程序不是图形化的

我不明白如何存储指针函数及其参数,并在run方法中调用此函数

谢谢你的帮助

计时器.h:

#ifndef TIMER_H
#define TIMER_H

#include <atomic>
#include <thread>
#include <chrono>
#include <iostream>
#include <list>

using namespace std;

class Timer {
private:
    atomic_bool done_ {};
    thread worker_{};
    //time interval
    chrono::milliseconds time;
    void run_();
    Fn** fn; // function called
    Args** args; // Argument of this function
public:
   template <class Fn, class... Args>
    Timer(chrono::milliseconds time, Fn&& fn, Args&&... args);
    ~Timer();
};

#endif // TIMER_H
\ifndef定时器
#定义定时器
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
班级计时器{
私人:
原子的{{};
线程工作者{};
//时间间隔
计时:毫秒时间;
void run_u1;();
Fn**Fn;//调用的函数
Args**Args;//此函数的参数
公众:
样板
计时器(计时:毫秒时间,Fn&&Fn,Args&&Args);
~Timer();
};
#endif//TIMER\u H
timer.cpp:

#include "timer.h"

template <class Fn, class... Args>
Timer::Timer(chrono::milliseconds time,Fn&& fn, Args&&... args){
    this->time = time;
    this->fn = fn;
    this->args = args;
    worker_ = thread(&Timer::run_,this);
}
// Thread method
void Timer::run_(){
    while(!this->done_.load()){
        //call function
        this_thread::sleep_for(time);
    }
}

Timer::~Timer(){
    done_.store(true);
    if(worker_.joinable())
        worker_.join();
    else
       cout << "thread termined" << endl;

}
#包括“timer.h”
样板
计时器::计时器(计时器::毫秒时间,Fn&&Fn,Args&&Args){
这->时间=时间;
这->fn=fn;
此->args=args;
worker=线程(&Timer::run,this);
}
//线程方法
无效计时器::运行{
而(!this->done_uz.load()){
//调用函数
此线程::睡眠(时间);
}
}
计时器::~Timer(){
完成存储(真);
if(worker_u2;.joinable())
worker_.join();
其他的

cout如果返回类型已知且不变,则可以使用
std::bind
std::function
来存储可调用对象以及
args
使用
std::bind
。但是需要一个通用结构来存储
std::bind
的结果,而
std::function
可以做到这一点

在类中声明一个
std::function
,并初始化它:

template <class Fn, class... Args>
Timer::Timer(chrono::milliseconds time, Fn&& fn, Args&&... args){
    static_assert(std::is_same_v<std::result_of_t<Fn(Args...)>, void>, "Return type must be void");

    this->time = time;

    auto callable = std::bind(fn, std::forward<Args>(args)...);
    this->func = std::function<void()>(callable);

    worker_ = thread(&Timer::run_,this);
}
模板
计时器::计时器(计时器::毫秒时间,Fn&&Fn,Args&&Args){
静态断言(std::is_same_v,“返回类型必须为void”);
这->时间=时间;
auto callable=std::bind(fn,std::forward and.

我不可能使用QTimer,因为我的程序不是图形化的:这显然是错误的。使用
std::function
并让调用方处理绑定参数。