zhinneg

#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 A
{
private:
    int n;

public:
    A(int m):n(m)
    {
        cout<<"constructor is called\n";
    }

    A(const A& a)
    {
        n = 3;
        cout<<"copy constructor is called\n";
    }

    A operator=(const A& a)
    {
        if(this == &a)
            return *this;
        n = a.n;
        cout<<"assign constructor is called\n";
        cout<<"this: "<<this<<endl;
        return *this;
    }

    void get()
    {
        cout<<"n: "<<n<<endl;
    }

    ~A()
    {}
};


int main()
{
    if(1)
    {
        int* n = new int[10];
        n[0] = 1;
        n[1] = 1;
        cout<<n[0]<<endl;
    }
    A* a = new A(8);

    A* b = new A(*a);

    A c = A(*a);

    A d = c;

    A f = A(10);

    A e = A(8); 

    cout<<"e: "<<&e<<endl;
    f.get();
    e = f;
    e.get();
    cout<<"e: "<<&e<<endl;

    while(1)
    {}
    //cout<<n[0]<<endl;
    return 0;
}

http://blog.chinaunix.net/uid-20296509-id-3365956.html

 

http://blog.csdn.net/guoliang624/article/details/6653001

 

http://blog.csdn.net/hackbuteer1/article/details/7561235

 

http://www.cnblogs.com/yangshaoning/archive/2012/03/18/cpp_smart_pointer.html

posted @ 2013-01-16 00:12  GOD_YCA  阅读(486)  评论(0)    收藏  举报