我试图利用boost :: function,我是C ++的新手,并且正在学习课程。
我有的:
class OptionPricer {
[constructor etc..]
double CallPrice(const double S);
double PutPrice(const double S);
double CallPrice(const double S, const double putPrice);
double PutPrice(const double S, const double callPrice);
vector<double> OptionPricer::Mesher(const double start, const double end, const double h, boost::function<double (const double)> funct);
};
我想使用boosts函数指针,因此不必复制Mesher函数,而只需传递* Price函数。
我尝试做的是:
boost::function<double (double)> functCP = &OptionPricer::CallPrice;
但是我得到:
ClCompile:OptionPricer.cpp Main.cpp
c:\ c ++ \ 9.a.1 \ 9.a.1 \ main.cpp(34):错误C2440:
'initializing':不能从'overloaded-function'转换为
'boost :: function'
与
[
签名=双(双)
]
没有构造函数可以采用源类型,或者构造函数重载解析度不明确
我也尝试了没有addressof运算符的尝试,但是我的经验太少,不足以全面了解此类问题。如果有人可以建议,那就太好了。
感谢您的评论,我之前也尝试过const double。
鉴于我现在尝试的建议,该建议奏效了,由此产生了另一个问题。
经过Mike Seymour的建议,我现在明白了:
// Calculate meshes
double start = 0, end = 20, h = 0.5;
boost::function<double (const double)> functCP = boost::bind(&OptionPricer::CallPrice, &pricer1, _1);
boost::function<double (const double)> functPP = boost::bind(&OptionPricer::PutPrice, &pricer1, _1);
cout << "Calculate meshes:" << endl;
cout << "Mesh1 Call: " << pricer1.Mesher(start, end, h, functCP) << " Mesh1 Put: " << pricer1.Mesher(start, end, h, functPP) << endl;
cout << "Mesh2 Call: " << pricer2.Mesher(start, end, h, functCP) << " Mesh2 Put: " << pricer2.Mesher(start, end, h, functPP) << endl;
cout << "Mesh3 Call: " << pricer3.Mesher(start, end, h, functCP) << " Mesh3 Put: " << pricer3.Mesher(start, end, h, functPP) << endl;
为什么我不想使用实例,是因为我随后需要将其绑定到所有三个Pricer实例,所以它们包含从中构建网格的数据。它们的实例值不同,我认为我可能需要使绑定更具动态性。
例如。上面的函数是不正确的,因为我仅在pricer2和pricer3实例上使用pricer1函数绑定。
我在现阶段要做的就是复制pricer2,pricer3等。实际上,我想摆脱冗余而不是复制Mesher,因为它只是在一个范围内进行迭代,现在我仍然可以复制,但肯定比复制更好功能方面的一种重复。
boost::function<double (const double)> functCP = boost::bind(&OptionPricer::CallPrice, &pricer2, _1);
boost::function<double (const double)> functPP = boost::bind(&OptionPricer::PutPrice, &pricer2, _1);
摆脱冗余,可能不是最技术性的解决方案,但:
boost::function<double (const double)> bindCPricer(OptionPricer &pricer) {
return boost::bind(&OptionPricer::CallPrice, &pricer, _1);
}
boost::function<double (const double)> bindPPricer(OptionPricer &pricer) {
return boost::bind(&OptionPricer::PutPrice, &pricer, _1);
}
cout << "Mesh1 Call: " << pricer1.Mesher(start, end, h, bindCPricer(pricer1)) << " Mesh1 Put: " << pricer1.Mesher(start, end, h, bindPPricer(pricer1)) << endl;
cout << "Mesh2 Call: " << pricer2.Mesher(start, end, h, bindCPricer(pricer2)) << " Mesh2 Put: " << pricer2.Mesher(start, end, h, bindPPricer(pricer2)) << endl;
非常感谢您的投入!
顺便说一句。我从事Java编程已有十多年了,我想知道为什么我以前没有学习过C ++,到目前为止,它对我来说似乎更强大,boost很棒,模板也比Java Generics更复杂。
最佳答案
function
对象需要自己调用,而成员函数需要一个对象。因此,您需要将其绑定到一个:
OptionPricer pricer; // The object you want the function to use
boost::function<double (double)> functCP =
boost::bind(&OptionPricer::CallPrice, &pricer, _1);
这会将函数绑定到指向
pricer
的指针,因此调用函数functCP(x)
等效于pricer.CallPrice(x)
。注意,如果对象是可复制的,则很容易意外地绑定到该对象的副本,这可能不是您想要的。另外,在现代C ++中,您可以使用lambda(并且您还可以使用标准库而不是Boost):
std::function<double (double)> functCP =
[&pricer](double x){return pricer.CallPrice(x);};
我发现它比绑定表达式更具可读性;您的美学可能会有所不同。
关于c++ - 成员函数的boost::function,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24065610/