#include<stdio.h> #include<memory> #include<iostream> using namespace std; template <class T> class smartpointer //智能指针的实现 { private: T *_ptr; public: smartpointer(T *p) : _ptr(p) //构造函数 { } T& operator *() //重载*操作符 { return *_ptr; } T* operator ->() //重载->操作符 { return _ptr; } ~smartpointer() //析构函数 { delete _ptr; } }; class data{ public: int a; int b; void fun(){ printf("data\n"); } void show(){ printf("a = %d b = %d\n",a,b); } ~data(){ printf("free\n"); } }; void Fun(){ smartpointer<data>m_example(new data()); m_example->fun(); m_example->a=1; m_example->b=1; m_example->show(); } int main(){ Fun(); getchar(); }