通过构造函数初始化成员函数 [英] Initialize member functions via constructor

查看:117
本文介绍了通过构造函数初始化成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我不在这个问题的左边,但是可以通过构造函数定义一个成员函数吗?

Perhaps I am way out of left field with this question, but is it possible to define a member function via the constructor?

在我的情况下,我正在尝试编写一个类来执行可靠的模型拟合(使用 RANSAC )。我希望这可以推广到不同类型的模型。例如,我可以使用它来确定对一组3D点的平面的估计。或者,也许我可以确定两组点之间的转换。在这两个示例中,可能需要不同的误差函数和不同的拟合函数。而不是使用类,静态函数调用可能看起来像是

In my case, I am trying to write a class to perform robust model fitting (using RANSAC). I want this to be generalizable to different types of models. For example, I could use this to determine an estimate of a plane to a set of 3D points. Or, perhaps I could determine a transformation between two sets of points. In these two examples, there might need to be different error functions and different fitting functions. Instead of using a class, a static function call might look like

model = estimate(data, &fittingFunc, &errorFunc);

我想知道我是否可以为这些模块化函数提供成员实例?

I'm wondering if I can have member instance for those modular functions?

类似

class Estimator
{
    private:
        // estimation params

        double errorFunc(std::vector<dtype>, double threshold); // Leave this unimplemented
        double fittingFunc(std::vector<dtype>, Parameters p); // Leave this unimplemented

    public:
        Estimator(void (*fittingFunc(std::vector<dtype>, Parameters), void (*errorFunc(std::vector<dtype>, double));

        dtype estimate(data); // Estimates model of type dtype. Gets implemented

};

Estimator::Estimator(void (*fittingFunc(std::vector<dtype>, Parameters), void (*errorFunc(std::vector<dtype>, double))
{
    fittingFunc = fittingFunc;
    errorFunc = errorFunc;
}

我想我在示例中混用了正确的语法,但我希望这个问题是明确的,基本上我在问:构造函数可以接受函数指针作为参数并将其分配为成员函数的实现吗?

I imagine I have bastardized the proper syntax in my example, but I hope the question is clear. Basically I am asking: Can the constructor accept function pointers as arguments and assign them to be the implementation of member functions?

其次,即使有可能,是否也将其视为错误的形式?

Secondly, even if this is possible, is it considered bad form?

更新:如果有帮助,请这里是用于鲁棒估计的MATLAB代码,它具有这种通用性的结构,我希望在C ++中复制

UPDATE: If it helps, here is MATLAB code for robust estimation that has this sort of generalizable structure I'm hoping to replicate in C++

推荐答案


构造函数可以接受函数指针作为参数并将其分配为成员函数的实现吗?

Can the constructor accept function pointers as arguments and assign them to be the implementation of member functions?

否。不是成员 functions 。但是您当然可以有公共成员函数指针

No. Not as member functions. But you can certainly have public member function pointers:

class Estimator
{
public:
    double (*errorFunc)(std::vector<dtype>, double threshold);
    double (*fittingFunc)(std::vector<dtype>, Parameters p);

public:
    Estimator(void (*fittingFunc(std::vector<dtype>, Parameters), void (*errorFunc(std::vector<dtype>, double))
    : errorFunc(errorFunc)
    , fittingFunc(fittingFunc)
    { }

    dtype estimate(data);    
};

要获得更好(或更安全)的界面,可以使函数指针 private 并具有一个简单地调用它们的公共成员函数。

For a nicer (or safer) interface, you can make the function pointers private and have a public member function which simply invokes them.

更一般而言,如果您对开销没问题,您可以具有类型 std :: function< double(std :: vector< dtype> ;, double)> std :: function< double(std :: vector< dtype> ;, Parameters)> ,然后您可以使用更多种可调用对象(函数指针,也可以是lambda,绑定成员函数等)

More generally, if you're okay with the overhead, you can have members of type std::function<double(std::vector<dtype>, double)> and std::function<double(std::vector<dtype>, Parameters)> and then you can use a wider variety of callables (function pointers, but also lambdas, bound member functions, etc.)

这篇关于通过构造函数初始化成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆