“间接调用指针所指的类成员的方法”,把我的代码列出如下:
(编译平台Windows2003,cygwin + g++3.4.1,vc++2003)
OperatorExClass.h(文件)------
// About ->* operator example.
class Class
{
public:
int Fun (void);
int m_var;
};
------
OperatorExClass.cpp------
// About ->* operator example.
#include <iostream>
#include "OperatorExClass.h"
int Class :: Fun (void) {
std :: cout << "Call function.\n";
std :: cout << m_var << "\n";
std :: cout << "End Call\n";
return 0;
}
------
OperatorEx.cpp------
// About ->* operator example.
#include "OperatorExClass.h"
int main (void) {
Class *pClass = new Class();
int (Class::*pFun) (void) = &Class::Fun; //注意括号
int (Class::*pm_var) = &Class::m_var;
(pClass->*pm_var) = 321;
(pClass->*pFun)();
delete pClass;
return 0;
}
------
还有个方便的技巧:
typedef int (Class::*PFUN) (void);
PFUN pFun = &Class::Fun;
本文地址:http://com.8s8s.com/it/it23778.htm