【发布时间】:2019-05-29 16:34:05
【问题描述】:
我已经可以在public: 标头中定义一个具有固定参数类型的函数指针向量,然后在构造函数中对其进行更新。但是,如果我希望能够传递带有任何类型参数的函数指针向量,如何在构造函数更新它之前定义它?
#include <iostream>
#include <vector>
class foo {
public:
std::vector<void (*)(int)> functions;
foo(std::vector<void (*)(int)> x) {
functions=x;
}
void run() {
functions[0](2);
}
};
void square(int n) { std::cout << n*n; }
int main() {
foo* bar=new foo(std::vector<void (*)(int)>{square});
bar->run();
return 0;
}
现在,如何将向量传递给任何类型的构造函数?
//snippet from above
std::vector<void (*)()> functions; //what do i do here?
template <typename T>
foo(std::vector<void (*)(T)> x) { //this works fine
functions=x;
}
【问题讨论】:
标签: c++ c++11 templates vector