开发者

C++ pointer to member function, declaration

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;}
...
 };

and pointer to the member function declared in another class:

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

How to declare/initlialize pointer to the member function for:

1] static class member function

Process.h

class Process
{
   private:
      static double ( Point2D :: *getCoord) () const; //How to initialize in Process.c开发者_JAVA百科pp?
      ...
};

2] non class member function

Process.h

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

class Process
{
   private:
      ...
};


The only thing you haven't done is to qualify the name of the function with the class name that it is a member of. Instead of providing a definition of Process::getCoord you've declared a global pointer-to-member called getCoord.

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

You can provide an initializer:

double ( Point2D::* Process::getCoord ) () const = &Point2D::getX;


According to the FAQ it's best to use typedef:

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

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

Initialization:

Process::getCoord = &Point2D::getX;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜