与方法的参数混淆?

时间:2014-03-20 21:51:17

标签: c++ opencv

我有一个具有以下结构的类:

class Detection
{
    public:
        void processImage (cv::Mat& image);
        void setGobalPositionCalculator (Point3D (*globalPositionCalculator) (float horizontalAngle, float verticalAngle));

    private:
        Point3D (*globalPositionCalculator) (float, float);

};

然后声明方法如下:

void Detection::setGobalPositionCalculator (Point3D (*globalPositionCalculator) (float, float))
{
    this->globalPositionCalculator = globalPositionCalculator;
}

void Detection::processImage(Mat& image)
{
        if (globalPositionCalculator)
        {
            landmark.position = globalPositionCalculator (hAngle, vAngle);
        }
}

问题1:

我无法理解setGobalPositionCalculator()的“参数”格式。我一直看到参数由comma( , )

分隔

问题2:

globalPositionCalculator不是该类的成员varibale,那么processImage()如何获取对它的访问权?

4 个答案:

答案 0 :(得分:3)

问题1

您看到的参数是function pointer,以下所有内容都是一个function pointer

Point3D (*globalPositionCalculator) (float horizontalAngle, float verticalAngle)

这是指该指针指向具有以下内容的方法:

   Point3D methodName(float horizontalAngle, float verticalAngle);
// ^^^^^^             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Return type^       arguments

您的globalPositionCalculator是指向methodName

的指针

cprogramming

查看此准系统示例
#include <iostream>
void my_int_func(int x)
{
    std::cout << x << std::endl;
}


int main()
{
    void (*foo)(int);
    foo = &my_int_func;

    /* call my_int_func (note that you do not need to write (*foo)(2) ) */
    foo( 2 );
    /* but if you want to, you may */
    (*foo)( 2 );

    return 0;
}

问题2

必须在<{em> processImage之后调用setGobalPositionCalculator方法,这是强制性的,否则不会发生任何事情。

现在,如果你仔细观察setGobalPositionCalculator

// I changed the function pointer variable name to add clarity to this example
void Detection::setGobalPositionCalculator (Point3D (*thefunctionpointer) (float, float))
{
    // In the next line a member variable of the Detection
    // object is set to the pointer you just passed
    // they are different variables
    this->globalPositionCalculator = thefunctionpointer;
} 

现在进入下一个方法

void Detection::processImage(Mat& image)
{
    // you are checking the private member
    if (this->globalPositionCalculator)
    {
        landmark.position = this->globalPositionCalculator (hAngle, vAngle);
    }
}

当然他们删除了this指针,因为它没有没有。但正如您所看到的,它增加了代码的清晰度。

答案 1 :(得分:1)

的参数类型
void Detection::setGobalPositionCalculator (Point3D (*globalPositionCalculator) (float, float))

是一个指向函数的指针,该函数需要两个float作为参数并返回Point3D

使用示例:

Point3D foo(float f1, float f2)
{
   Point3d p;
   // Do the necessary calculations to set the data
   // of p by using f1 and f2

   return p;
}

Detection d;
d.setGlobalPositionCalculator(foo);

答案 2 :(得分:1)

globalPositionCalculator是指向函数的指针,它会获得两个float个数字,并返回Point3D。为了更好地理解,您可以在这里开始阅读Function_pointer

撰写此class的人实际上希望您知道如何计算global Position,因此您需要使用给定的原型编写函数,并将其作为参数传递给setGobalPositionCalculator方法,后来他在处理图像时使用该逻辑来计算界标位置。

编辑:回答您的评论
代码说明有地址。函数名实际上是函数第一条指令的地址。该类中的某个地方应该是函数指针globalPositionCalculator的定义。这句话

       `this->globalPositionCalculator = globalPositionCalculator;` 

指定一些逻辑的地址,该逻辑知道如何计算全局位置到eth class memer globalPositionCalculator,它是指向函数的指针。稍后在processImage方法中,程序将跳转到该段代码,处理它并将获取它的返回值,processImage将继续其流程。

答案 3 :(得分:1)

此:

void Detection::setGobalPositionCalculator (Point3D (*globalPositionCalculator) (float, float))

也可以通过以下方式表达:

//// an "alias" used when this type of function pointer is needed a lot
typedef Point3D (*globalPositionCalculator) (float, float);

void Detection::setGobalPositionCalculato(globalPositionCalculator funct_ptr);

你的意思是“成员”,只需调用function_ptr:

就可以获得成功
Point3D myFunc(float x, float y); // declaration --> must be implemented

globalPositionCalculator atPos = NULL; // init
atPos = &myFunc; // this is basically what Detection::setGlobal[...] does.

float m_X, m_Y;
atPos(m_X, m_Y);  // call function (e.g. with object params).

在课堂上:

 class A {
    public:
        void setter(globalPositionCalculator func_ptr) { m_Ptr = funct_ptr; }
        void callFunc() { if (m_Ptr != NULL) { m_Ptr(m_X, m_Y); }

   private:
      float x;
      float y;
      globalPositionCalculator m_Ptr;
};