#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <memory>
using namespace std;
class A
{
public:
A(int a) {
cout << "A(int)" << endl;
this->a = a;
}
void func(){
cout << "a = " << a << endl;
}
~A() {
cout << "~A()" << endl;
}
private:
int a;
};
class MyAutoPtr
{
public:
MyAutoPtr(A * p)
{
this->ptr = p;
}
~MyAutoPtr() {
if (this->ptr != NULL) {
delete this->ptr;
}
}
//重载->操作符
A* operator->()
{
return this->ptr;
}
//重载*操作符
A& operator*()
{
return *ptr;
}
private:
A * ptr;
};
//智能指针
void test1()
{
#if 0
A* p = new A(10);
p->func();
delete p;
#endif
#if 0
auto_ptr<A> ptr(new A(10));
ptr->func();
#endif
MyAutoPtr myPtr(new A(10));
//myPtr->func();
(*myPtr).func();
}
int main(void)
{
test1();
return 0;
}