Confused with method arguments?

I have a class with the following structure:

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

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

};

Then the methods are declared as follows:

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

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

Problem 1:

I can not understand the format of "Argument" setGobalPositionCalculator(). I have always seen that arguments are divided intocomma( , )

Problem 2:

globalPositionCalculatoris not a member of varibale for a class, then how processImage()to access it?

+4
source share
4 answers

Problem 1

The arguments you see are function pointer, all of the following: ONEfunction pointer

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

This pointer is said to point to a method that has the following:

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

And yours globalPositionCalculatoris a pointer pointing tomethodName

Check out this example with barebones from 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;
}

Problem 2

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, . , , .

+3

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

, float a 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);
+1

globalPositionCalculator , float Point3D. , Function_pointer

, class, , , global Position, setGobalPositionCalculator, .

:
. - . - globalPositionCalculator.

       `this->globalPositionCalculator = globalPositionCalculator;` 

assigns the address of some piece of logic that knows how to calculate the global position for eth class memer globalPositionCalculator, which is a pointer to a function. Later in the method, the processImageprogram will go to this part of the code, process it and accept the return value, and processImage will continue its flow.

+1
source

It:

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

can also be expressed in terms of:

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

void Detection::setGobalPositionCalculato(globalPositionCalculator funct_ptr);

The "member" you mean can simply be called by 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).

In the class:

 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;
};
+1
source

Source: https://habr.com/ru/post/1532735/


All Articles