1. 程式人生 > >過載->和*操作符之operator->()與operator*()

過載->和*操作符之operator->()與operator*()

#include<iostream>
using namespace std;

class Action
{
public:
	void action()
	{
		cout<<"Hello World"<<endl;
	}
};

class Wrapper
{
public:
	Wrapper(Action* _it):it(_it){}
	Action* operator->(){ return it;}
	Action& operator*(){ return *it; }
private:
	Action* it;
};
int main()
{
	Action a;
	Wrapper w(&a);  
	w->action();    //解釋成 (w.operator->())->action();
	(*w).action();  //解釋成 (w.operator*()).action();
	(w.operator->())->action();
	(w.operator*()).action();
}

/*
Hello World
Hello World
Hello World
Hello World
*/