2010-11-18 22 views
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 

    } 

} 

ありがとうございました。あなたは、ポインタを経由してメンバ関数を呼び出すために->*演算子を使用する必要が

+0

今(私は、状態(状態パターン)を使用しますが、このために、それは大丈夫そうですジェームスは解決策を与えた) – stefaanv

答えて

6

:やや複雑な状況のために

(point->*getCoordinate)(); 
関連する問題