Member Function Pointer

I have the following class:

class Point2D
{
    protected:

            double x;
            double y;
    public:
            double getX() const {return this->x;}
            double getY() const {return this->y;}
   ...

};

Sometimes I need to return the x coordinate, sometimes the y coordinate, so I use a pointer to the member function getX (), getY (). But I can’t return the coordinate, see below, please.

double ( Point2D :: *getCoord) () const;

class Process
{
   ......
   public processPoint(const Point2D *point)
   {

      //Initialize pointer
      if (condition)
      {
         getCoord = &Point2D::getX;
      }
      else
      {
         getCoord = &Point2D::getY;
      }

      //Get coordinate
      double coord = point->( *getCoordinate ) (); //Compiler error

   }

}

Thank you for your help.

+3
source share
1 answer

You need to use an operator ->*to call a member function using a pointer:

(point->*getCoordinate)(); 
+6
source

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


All Articles