如何使用函数指针作为类中的参数

时间:2016-01-13 22:50:59

标签: c++ templates asynchronous future

我正在尝试构建一个本质上是期货队列的类,这些类都是异步处理的,并且当main想要获取它们的值时最终存储。我在创建接受这些函数及其参数的函数参数时遇到了麻烦,以便创建异步操作,然后将其推入到期货队列中。有问题的区域是调用构造函数和成员函数#include <iostream> #include <queue> #include <future> #include <thread> using namespace std; using longInt = unsigned long long int; //prototypes longInt isPrime(longInt); template <typename return_type> class TaskQueue { private: queue<future<return_type>> tasks; public: //return a copy of the queue queue<future<return_type>> copy() const { return tasks; } //default constructor //does nothing, waits for input TaskQueue() { //do nothing } //call constructors //adds task to queue TaskQueue(return_type (*func)(), Args&& ... args) { tasks.push(new async(func, args)); } //copy constructor //copies another queue to this one TaskQueue(const queue<future<return_type>> & in) { tasks = in.copy(); } //setter and getter functions //inserts a new task into the queue void add(return_type(*func)(), Args&& ... args) { tasks.push(new aync(in, args)); } //returns true if the task at the top of the queue is ready bool valid() { return tasks.front().valid(); } //gets the value, if the value is not ready, waits for it to be ready //pops the top task after getting it return_type get() { return_type temp = tasks.top().get(); tasks.pop(); return temp; } //waits for the value of the top of the queue to become ready void wait() { tasks.top().wait(); } }; int main() { TaskQueue<longInt> checkPrimes; checkPrimes.add(isPrime, 5); longInt test = checkPrimes.get(); cout << test << endl; } //returns the number if it is prime or 0 if it is not longInt isPrime(longInt n) { if (n <= 3) { if (n > 1) { return n; } return 0; } if (n % 2 == 0 || n % 3 == 0) { return 0; } for (unsigned short i = 5; i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) { return 0; } } return n; } 。以下是我到目前为止的情况:

projectB/
  projectB_file.py
  projectA/        (repository)
    projectA/      (source code)
      module1/
        file1.py   (contains Class1)
      file2.py     (contains Class2)
    tests/
      test_file1.py

2 个答案:

答案 0 :(得分:1)

可编辑的版本:

template <typename return_type>
class TaskQueue {
private:
    queue<future<return_type>> tasks;
public:
    //return a copy of the queue
    queue<future<return_type>> copy() const {
        return tasks;
    }


    //default constructor
    //does nothing, waits for input
    TaskQueue() {
        //do nothing
    }

    //call constructors
    //adds task to queue
    template <typename ... Args, typename ... Ts>
    TaskQueue(return_type (*func)(Ts...), Args&& ... args) {
        tasks.push(std::async(func, args...));
    }

    //copy constructor
    //copies another queue to this one
    TaskQueue(const queue<future<return_type>> & in) {
        tasks = in.copy();
    }

    //setter and getter functions

    //inserts a new task into the queue
    template <typename ... Args, typename ... Ts>
    void add(return_type(*func)(Ts...), Args&& ... args) {
        tasks.push(std::async(func, args...));
    }

    //returns true if the task at the top of the queue is ready
    bool valid() {
        return tasks.front().valid();
    }

    //gets the value, if the value is not ready, waits for it to be ready
    //pops the top task after getting it
    return_type get() {
        return_type temp = tasks.front().get();

        tasks.pop();

        return temp;
    }

    //waits for the value of the top of the queue to become ready
    void wait() {
        tasks.top().wait();
    }

};

Demo

答案 1 :(得分:0)

我认为您可以为要传递的函数定义一个类型,然后按照您想要的顺序应用它们。例如,以下代码段将定义intfunc类型,该类型为int并返回int

typedef int (*intfunc) (int);

然后我们可以定义像

这样的函数
int sum2(int n) {
    return n + 2;
}

int mult3(int n) {
    return 3 * n;
}

并将它们交给矢量

vector<intfunc> funcs;
vector<intfunc>::iterator func;
int start = 0;

funcs.push_back(sum2);
funcs.push_back(mult3);

cin >> start;
for (func = funcs.begin(); func != funcs.end(); ++func)
{
    start = (*func)(start);
}

cout << start << endl;

如果我们为该程序输入5,它将按预期返回21。

相关问题