#include <stdio.h> #include <stdlib.h> class Temp { public: Temp() { printf("%s:构造函数\n", __FUNCTION__); } ~Temp() { printf("%s:析构函数\n", __FUNCTION__); } void func() { printf("%s\n", __FUNCTION__); } private: }; template <class T> class SmartPointer { public: SmartPointer(){} SmartPointer(T* data) { p = data; } ~SmartPointer() { delete p; } //重载->运算符 T* operator->() { return p; } T& operator*() { return *p; } private: T* p; };
int main(int argc, char* argv[]) { SmartPointer<Temp> sp(new Temp); //sp.operator->()->func(); sp->func(); (*sp).func(); getchar(); return 0; }
标签:__,SmartPointer,Temp,sp,C++,智能,func,printf,指针 From: https://www.cnblogs.com/caiyingyong/p/17158777.html