過載->和*操作符之operator->()與operator*()
阿新 • • 發佈:2019-02-13
#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 */