![](/img/trans.png)
[英]boost::bind & boost::function pointers to overloaded or templated member functions
[英]boost::function on member functions
我試圖利用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:'正在初始化':無法從'重載函數'轉換為'boost :: function'with [Signature = double(double)]沒有構造函數可以采用源類型,或者構造函數重載分辨率不明確
我也嘗試了沒有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
函數綁定到指向pricer
的指針,因此調用函數functCP(x)
等同於pricer.CallPrice(x)
。 請注意,如果對象是可復制的,則很容易意外地綁定到該對象的副本,這可能不是您想要的。
另外,在現代C ++中,您可以使用lambda(並且您還可以使用標准庫而不是Boost):
std::function<double (double)> functCP =
[&pricer](double x){return pricer.CallPrice(x);};
我發現它比綁定表達式更具可讀性; 您的美學可能會有所不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.