//这程序,刚看的时候好费劲啊,贴上来大家分享一下
#include
using namespace std;
class M
{
int x;
int y;
public:
void set(int a,int b)
{
x=a;
y=b;
}
friend int sum(M m);
};
int sum(M m)
{
int M::* px = &M::x; //M::*意为“指向类M的成员的指针”
int M::* py = &M::y;
M* pm = &m;
int s=m.*px + pm->*py;
return s;
}
int main()
{
M n;
void (M::*pf)(int ,int)=&M::set;
(n.*pf)(10,20);
cout << "sum" << sum(n) << endl;
M *op = &n;
(op->*pf)(30,40);
cout << "sum" << sum(n) << endl;
return 0;
}
//结果sum30
//sum70
阅读(1158) | 评论(0) | 转发(0) |