[英]Pointer to member function
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;}
...
};
有时我需要返回x坐标,有时是y坐标,所以我使用指向成员函数getX(),getY()的指针。 但我无法返回坐标,请参阅下文。
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
}
}
谢谢你的帮助。
您需要使用->*
运算符通过指针调用成员函数:
(point->*getCoordinate)();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.