c ++将数据源函数作为参数传递
c++ pass data source function as parameter
我有一个静态函数:
void E::createEP(std::list<double>* ret, double length, double (*getHeight)(double)) {
// magic
double sampleIntervall = //from magic
double dist = 0;
while (length - dist > -0.1) {
ret->push_back(dist, (*getHeight)(dist));
dist += sampleIntervall;
}
}
在不同的调用中,我有两个不同的函数作为 getHeight() 传递:
- 数据库包装器中的函数 class 不是静态的
- 与
createEP()
类型 E
相同的对象中的函数(对于具有不同参数的 "recreating"),它需要访问元素字段,因此也不能是静态的.
是否可以通过某种方式将非静态函数作为参数传递给静态函数?
如果不是,最优雅的选择是什么?我不想复制 createEP()
.
中的整个代码
对于 c++11,最好的选择可能是使用 std::function<double(double)>
而不是原始函数指针。
您可以使用的是 std::function
。它将可调用类型包装为函数、成员函数、具有重载函数运算符的对象。您只需提供要使用的签名。所以在你的情况下你会有
void E::createEP(std::list<double>* ret, double length, std::function<double(double)> getHeight)
然后将成员函数与您要调用它的对象绑定,您可以使用 lambda like
[&](double val){ return object.FunctionName(val); }
或std::bind
喜欢
std::bind(&ClassName::FunctionName, &object)
std::function
在这里真的很有用
看到这个:
#include <functional>
using funType = std::function<void(double)>;
struct foo
{
void bar(double d) {};
static void call(funType f) {};
};
void foo2(double)
{
}
//...
foo f;
//create lambda which calls member function
auto f1 = [&f](double d) { f.bar(4); };
foo::call(f1); //indirect call to member function due to lambda
foo::call(&foo2); //call to non-member function.
//...
std::function
可以保存函数指针或 lambda,这与 C 风格的函数指针相比非常好。
您也可以直接使用成员函数调用它,但这需要 bind
。
我认为将调用包装在 lambda 中的解决方案看起来更好一些。
替代std::function
void E::createEP(std::list<double>* ret,
double length,
const std::function<double (double)>& getHeight)
您可以直接使用模板
template <typename F>
void E::createEP(std::list<double>* ret,
double length,
F&& getHeight)
两种情况下都有正文
{
// magic
double sampleIntervall = //from magic
double dist = 0;
while (length - dist > -0.1) {
ret->push_back(dist, getHeight(dist));
dist += sampleIntervall;
}
}
我有一个静态函数:
void E::createEP(std::list<double>* ret, double length, double (*getHeight)(double)) {
// magic
double sampleIntervall = //from magic
double dist = 0;
while (length - dist > -0.1) {
ret->push_back(dist, (*getHeight)(dist));
dist += sampleIntervall;
}
}
在不同的调用中,我有两个不同的函数作为 getHeight() 传递:
- 数据库包装器中的函数 class 不是静态的
- 与
createEP()
类型E
相同的对象中的函数(对于具有不同参数的 "recreating"),它需要访问元素字段,因此也不能是静态的.
是否可以通过某种方式将非静态函数作为参数传递给静态函数?
如果不是,最优雅的选择是什么?我不想复制 createEP()
.
对于 c++11,最好的选择可能是使用 std::function<double(double)>
而不是原始函数指针。
您可以使用的是 std::function
。它将可调用类型包装为函数、成员函数、具有重载函数运算符的对象。您只需提供要使用的签名。所以在你的情况下你会有
void E::createEP(std::list<double>* ret, double length, std::function<double(double)> getHeight)
然后将成员函数与您要调用它的对象绑定,您可以使用 lambda like
[&](double val){ return object.FunctionName(val); }
或std::bind
喜欢
std::bind(&ClassName::FunctionName, &object)
std::function
在这里真的很有用
看到这个:
#include <functional>
using funType = std::function<void(double)>;
struct foo
{
void bar(double d) {};
static void call(funType f) {};
};
void foo2(double)
{
}
//...
foo f;
//create lambda which calls member function
auto f1 = [&f](double d) { f.bar(4); };
foo::call(f1); //indirect call to member function due to lambda
foo::call(&foo2); //call to non-member function.
//...
std::function
可以保存函数指针或 lambda,这与 C 风格的函数指针相比非常好。
您也可以直接使用成员函数调用它,但这需要 bind
。
我认为将调用包装在 lambda 中的解决方案看起来更好一些。
替代std::function
void E::createEP(std::list<double>* ret,
double length,
const std::function<double (double)>& getHeight)
您可以直接使用模板
template <typename F>
void E::createEP(std::list<double>* ret,
double length,
F&& getHeight)
两种情况下都有正文
{
// magic
double sampleIntervall = //from magic
double dist = 0;
while (length - dist > -0.1) {
ret->push_back(dist, getHeight(dist));
dist += sampleIntervall;
}
}