指向成员函数的指针
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
}
}
感谢您的帮助。
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 x coordinate, sometimes y coordinate, so I am using pointer to the member function getX(), getY(). But I am not able tu return 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
}
}
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
->*
运算符通过指针调用成员函数:You need to use the
->*
operator to call a member function via a pointer: