STL---auto_ptr实现

来源:互联网 发布:java怎么写日志 编辑:程序博客网 时间:2024/05/22 13:48
#include<iostream>



using namespace std;



template<class T>
class auto_ptr1
{
        public:
                explicit auto_ptr1(T* p =0 ):pointee(p){ cout<<"p..\n";}


                template<class U>
                auto_ptr1(auto_ptr1<U>&rhs):pointee(rhs.release()){cout<<"rhs..\n";}
                ~auto_ptr1()
                {
                        cout<<"~\n";
                        delete pointee;
                }


                template<class U>
                auto_ptr1<T>& operator=(auto_ptr1<U>&rhs)
                {
                        cout<<"=\n";
                        if(this != &rhs)
                                reset(rhs.release());
                        return *this;
                }


                T& operator*()const
                {
                        cout<<"*\n";
                        return *pointee;
                }
                T* operator->()const
                {
                        cout<<"->\n";
                        return pointee;
                }
                T* get()const
                {
                        return pointee;
                }
                //...
        private:
                T* pointee;
};


void test()
{
        auto_ptr1<string> ps (new string("jjhou"));
        cout<<*ps<<endl;


        cout<<ps->size()<<endl;
}


int main()
{
        test();
}
原创粉丝点击