如何将函数指针用作类中的参数
How to use Function Pointer as a parameter in a Class
我正在尝试构造一个类,该类本质上是一个期货队列,这些期货都是异步处理的,最终在 main 想要获取其值时全部存储。我在创建将接受这些函数及其参数的函数参数时遇到问题,以便创建一个异步操作,然后将其推入期货队列。有问题的领域是调用构造函数和成员函数add()
。 这是我到目前为止所拥有的:
#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;
}
一个可编译的版本:
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();
}
};
演示
我认为您可以为要传递的函数定义一个类型,然后按所需的顺序应用它们。例如,以下代码片段将定义一个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。
相关文章:
- 将可变参数函数的参数封装在类实例中
- QML 使用带有参数C++函数
- 使用可变参数函数作为模板参数
- 如何在C++中伪造虚拟可变参数函数模板?
- 为什么可变参数函数不适用于模板
- C++ std::functional 中的可变参数函数模板
- 可变参数函数指针的定义对于VxWorks spyLib来说不清楚
- 使用可变参数函数覆盖具有不同函数签名的虚函数
- 考虑引用和常量的可变参数函数包装器
- 使用可变参数函数将整数和/或整数数组放入单个 int 数组中
- 在可变参数函数中转发特定范围的参数
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 使用带有一个参数函数的递归找到数字的平方
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 多个可变参数函数的单个模板参数包?
- 参数数据类型未知的可变参数函数
- 可变参数函数参数包扩展
- 使用模板可变参数函数将多个参数传递给另一个函数
- 对可变参数函数的递归调用的链接器错误
- 通过像printf这样的可变参数函数传递一个带有常量字符*转换函数的类