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

根据常见问题解答,最好使用typedef

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

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

初始化:

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