3
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
}
}
感谢您的帮助。
对于稍微复杂的情况下,我将使用状态(状态模式),但对于这一点,似乎还好(现詹姆斯已经给出了解决方案) – stefaanv 2010-11-18 16:26:16