c++ - 成员函数上的 boost::function

标签 c++ boost

我正在尝试使用 boost::function,我是 C++ 的新手并且正在学习一门类(class)。

我有什么:

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): error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'boost::function' with [ Signature=double (double) ] No constructor could take the source type, or constructor overload resolution was ambiguous

我也尝试过不使用 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;

为什么我不想使用一个实例是因为我需要将它绑定(bind)到所有 3 个 pricer 实例,它们包含从例如构建网格的数据。它们的实例值不同,我想我可能需要以某种方式使绑定(bind)更加动态。

例如上面的函数不正确,因为我在 pricer2 和 pricer3 实例上只使用了 pricer1 函数绑定(bind)。

我在当前阶段要做的是为 pricer2、pricer3 等复制它。实际上我想摆脱冗余而不是复制网格器,因为它只是在一个范围内迭代,现在我仍然有一些重复,但肯定比在功能方面有一种重复要好。

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 超过 10 年了,我想知道为什么我没有早点学习 C++,到目前为止它对我来说看起来更强大,boost 很棒,模板也比 Java Generics 更复杂。

最佳答案

function 对象需要自己调用,而成员函数需要一个对象。所以你需要将它绑定(bind)到一个:

OptionPricer pricer; // The object you want the function to use
boost::function<double (double)> functCP = 
    boost::bind(&OptionPricer::CallPrice, &pricer, _1);

这将函数绑定(bind)到指向 pricer 的指针,因此调用函数 functCP(x) 等同于 pricer.CallPrice(x)。请注意,如果对象是可复制的,则很容易意外绑定(bind)到它的拷贝,这可能不是您想要的。

或者,在现代 C++ 中,您可以使用 lambda(并且您还可以使用标准库而不是 Boost):

std::function<double (double)> functCP = 
    [&pricer](double x){return pricer.CallPrice(x);};

我发现它比绑定(bind)表达式更具可读性;您的审美可能会有所不同。

关于c++ - 成员函数上的 boost::function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24065610/

相关文章:

c++ - 统计大数据流中每个元素出现的次数

c++ - boost::asio 如何以正确的方式读取完整缓冲区?

c++ - MC 模拟中的 openmp 私有(private)/共享数据

c++ - boost 互斥量和类成员访问

c++ - SIGPIPE,断管

c++ - 期待来自 std::cin (Unix C++) 的输入

c++ - 编译 C++ 项目/Open GL 时出现纹理错误

c++ - BOOST 程序_选项 : parsing multiple argument list

c++ - C++ 中的不可变类

c++ - Qt Creator:如何运行Windows批处理文件并获得结果