operator->*运算符重载程序分析

本文详细解析了C++中如何使用成员函数指针和智能对象(FunctionObject)来实现灵活的函数调用。通过一个Dog类的实例,展示了如何定义和使用成员函数指针类型PMF,并通过智能对象的构造函数和operator()操作符,实现了对不同成员函数的动态调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

程序代码

如下所示:

#include<iostream>
using namespace std;

class Dog
{
public:
	int run(int i) const { cout << "run\n"; return i; }
	int eat(int i) const { cout << "eat\n"; return i; }
	int sleep(int i) const { cout << "ZZZ\n"; return i; }
	typedef int(Dog::*PMF)(int) const; 
	class FunctionObject
	{
		Dog* ptr;
		PMF pmem;
	public:
		FunctionObject(Dog* wp, PMF pmf) :ptr(wp), pmem(pmf){ cout << "FunctionObject constructor\n"; }
		int operator()(int i) const
		{
			cout << "FunctionObject::operator()\n";
			return (ptr->*pmem)(i);
		}
	};
	FunctionObject operator->*(PMF pmf)
	{
		cout << "operator->*" << endl;
		return FunctionObject(this, pmf);
	}
};

int main()
{
	Dog w;
	Dog::PMF pmf = &Dog::run;
	cout << (w->*pmf)(1) << endl;
	pmf = &Dog::sleep;
	cout << (w->*pmf)(2) << endl;
	pmf = &Dog::eat;
	cout << (w->*pmf)(3) << endl;
	return 0;
}

程序分析

1.typedef int(Dog::*PMF)(int) const;

这句是用于简化定义的。新的类型名是PMF,是一个函数指针。其取代的类型表示Dog类的成员函数。如果将*PMF替换为特定的成员函数名,则与函数定义相同。故其格式与成员函数格式相似。

2.程序运行结果分析

运行结果如下图所示:
在这里插入图片描述

cout << (w->*pmf)(1) << endl;

这句中:

  1. 调用operator->*函数,因此打印相应的字符串
  2. 此函数返回一个FunctionObject对象,在复制过程中调用其构造函数,因此打印出第二行的字符串
  3. 由于随后跟了个(1),因此调用了FunctionObject的operator()函数,因此打印出第三行的字符串
  4. operator()函数返回(ptr->*pmem)(i),此返回值中,*pmemDog::PMF pmf = &Dog::run;,因此是等价于w.run(i),故有第四行内容

其余各行原理同上。

我正在编辑C++代码 ,请帮我检查并改正错误点。我的原始代码如下: #include<bits/stdc++.h> using namespace std; const int N=1e5+5; const int M=39989; const double eps=1e-9; class xdtr{ public: class xd{ public: int id; int xl,xr; double a,b; xd(int id,int x1,int y1,int x2,int y2){ xd::id=id; if(x1>x2)swap(x1,y1); xl=x1; xr=x2; if(x1==x2){ a=0; b=max(y1,y2); }else{ a=((double)x2-x1)/(y2-y1); b=y1-x1*a; } } double operator&(int d){ if((xl<=d)&&(d<=xr))return a*d+b; else return -1e9; } friend xd* max(xd* x,xd*y,int d){ if(x==nullptr)return y; if(y==nullptr)return x; double w=x->operator&(d)-y->operator&(d); if(abs(w)<=eps){ return (x->id<=y->id)?x:y; } return (w>0)?x:y; } }; class Node{ public: int l,r; xd *lz; Node *cl,*cr; int mid(){ return (l+r)>>1; } }; Node* add(int l,int r){ return new Node{l,r,nullptr,nullptr,nullptr}; } Node *head; xdtr(int l,int r){ head=add(l,r); } Node* zl(Node *x){ if(x->cl==nullptr)x->cl=add(x->l,x->mid()); return x->cl; } Node* zr(Node *x){ if(x->cr==nullptr)x->cr=add(x->mid()+1,x->r); return x->cr; } xd* query(Node *x,int d){ if(x==nullptr)return nullptr; xd* ans=x->lz; if(d<=x->mid())ans=max(ans,query(x->cl,d),d); else ans=max(ans,query(x->cr,d),d); return ans; } void update(Node *x,xd *d){ if(max(x->lz,d,x->mid())==d)swap(x->lz,d); if(max(x->lz,d,x->l)==d)update(zl(x),d); if(max(x->lz,d,x->r)==d)update(zr(x),d); } void insert(Node *x,xd *d){ if((d->xl<=x->l)&&(x->r<=d->xr))update(x,d); if(d->xl<=x->mid())insert(zl(x),d); if(d->xl>x->mid()) insert(zr(x),d); } xd* query(int x){ return query(head,x); } void insert(xd x){ insert(head,new xd(x)); } }tr(1,M); int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T,h=0,la=0; cin>>T; while(T--){ int op; cin>>op; if(op==0){ int k; cin>>k; k=(k+la-1)%M+1; cout<<(tr.query(k))->id<<endl; }else{ int x1,y1,x2,y2; cin>>x1>>y1>>x2>>y2; x1=(x1+la-1)%M+1; y1=(y1+la-1)%M+1; x2=(x2+la-1)%M+1; y2=(y2+la-1)%M+1; tr.insert(xdtr::xd(++h,x1,y1,x2,y2)); } } return 0; }
最新发布
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值