2

私は次のクラスを持っています:

class Point2D
{
protected:

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

別のクラスで宣言されたメンバー関数へのポインター:

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

メンバ関数へのポインタを宣言/初期化する方法:

1]静的クラスメンバー関数

Process.h

class Process
{
   private:
      static double ( Point2D :: *getCoord) () const; //How to initialize in Process.cpp?
      ...
};

2]非クラスメンバー関数

Process.h

double ( Point2D :: *getCoord) () const; //Linker error, how do declare?

class Process
{
   private:
      ...
};
4

2 に答える 2

3

まだ行っていないのは、関数の名前を、そのメンバーであるクラス名で修飾することだけです。の定義を提供する代わりに、Process::getCoordと呼ばれるメンバーへのグローバルポインタを宣言しましたgetCoord

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

イニシャライザを提供できます。

double ( Point2D::* Process::getCoord ) () const = &Point2D::getX;
于 2010-11-20T21:15:25.883 に答える
1

FAQによると、使用するのが最善typedefです:

typedef double (Point2D::*Point2DMemFn)() const;

class Process
{
      static Point2DMemFn getCoord;
      ...
};

初期化:

Process::getCoord = &Point2D::getX;
于 2010-11-20T18:44:00.627 に答える