#include <iostream> using std::cout; using std::endl; class CBase { public: CBase() { } virtual void Draw(const char *str) = 0; }; class CRealClass: public CBase { public: CRealClass() { cout << \"CRealClass construct\" << endl; } virtual void Draw(const char *str) { cout << \"CRealClass draw:\" << str << endl; } }; class CProxy: public CBase { public: CProxy():ptrFilePath(NULL) { pReal = NULL; } virtual void Draw(const char *str) { if(pReal) { pReal->Draw(str); } else { } } CRealClass *LoadRealClass() { if(!pReal) { // create real class object simply using constructor pReal = new CRealClass(); } return pReal; } ///重载->运算符为何没有生效????????????? CRealClass *operator-> () { cout << \"operator -> be called!\" << endl; return LoadRealClass(); } private: // real object pointer CRealClass *pReal; const char *ptrFilePath; }; int main(int argc, char *argv[]) { //real class won’t be generate while using proxy CBase *ptrObj = new CProxy(); ptrObj->Draw(\"asdf\"); return 0; } |