C++指向成员函数的指针、声明
我有以下类:
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:
...
};
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.cpp?
...
};
2] non class member function
Process.h
double ( Point2D :: *getCoord) () const; //Linker error, how do declare?
class Process
{
private:
...
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您唯一没有做的事情是使用函数所属的类名来限定函数的名称。您没有提供
Process::getCoord
的定义,而是声明了一个名为getCoord
的全局成员指针。您可以提供一个初始化程序:
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 calledgetCoord
.You can provide an initializer:
根据常见问题解答,最好使用
typedef
:初始化:
According to the FAQ it's best to use
typedef
:Initialization: