• 理解smart pointer之二:如何实现一个smart pointer


    大家都用过smart pointer,但是如果要自己实现一个,有哪些函数需要实现呢,下面是auto_ptr的实现代码,我加了几行注释,帮助理解,同时列举哪些函数需要实现:

    1. explicit版的构造函数;

    2. 类型转换操作符,可以使auto_ptr<子类>到auto_ptr<父类>的赋值;

    3. Copy构造函数;

    4. pointer-to-member 操作符(*  and –>);

    5. 析构函数;

    template<class _Ty>
    class auto_ptr
    {    // wrap an object pointer to ensure destruction
    public:
        typedef auto_ptr<_Ty> _Myt;
        typedef _Ty element_type;

        explicit auto_ptr(_Ty *_Ptr = 0) throw() // 【Must】使用explicit,避免该构造函数成为隐式类型转换构造函数
            : _Myptr(_Ptr) // initialize the member in the initialization list, instead of calling (default constructor +assignment)
        {    // construct from object pointer
        }

        auto_ptr(_Myt& _Right) throw()  //【Must】Copy构造函数,auto_ptr的行为是将移除操作符右边的对象对资源的所有权
            : _Myptr(_Right.release())
        {    // construct by assuming pointer from _Right auto_ptr
        }

        auto_ptr(auto_ptr_ref<_Ty> _Right) throw()
        {    // construct by assuming pointer from _Right auto_ptr_ref
            _Ty *_Ptr = _Right._Ref;
            _Right._Ref = 0;    // release old
            _Myptr = _Ptr;    // reset this
        }

        template<class _Other>
        operator auto_ptr<_Other>() throw()  //【Must】隐式类型转换操作符,可以实现:auto_ptr<A> ptra(new A());auto_ptr<B> ptrb(new B()); ptra = ptrb; 这时要求B能隐式转换为A,比如B是A的子类;
        {    // convert to compatible auto_ptr
            return (auto_ptr<_Other>(*this));
        }

        template<class _Other>
        operator auto_ptr_ref<_Other>() throw()
        {    // convert to compatible auto_ptr_ref
            _Other *_Cvtptr = _Myptr;    // test implicit conversion
            auto_ptr_ref<_Other> _Ans(_Cvtptr);
            _Myptr = 0;    // pass ownership to auto_ptr_ref
            return (_Ans);
        }

        template<class _Other>
        _Myt& operator=(auto_ptr<_Other>& _Right) throw()//【Must】赋值操作符,其行为跟Copy构造函数一致
        {    // assign compatible _Right (assume pointer)
            reset(_Right.release());
            return (*this);
        }

        template<class _Other>
        auto_ptr(auto_ptr<_Other>& _Right) throw() //【Must】Copy构造函数,可以实现auto_ptr<子类>到auto_ptr<父类>的转化
            : _Myptr(_Right.release())
        {    // construct by assuming pointer from _Right
        }

        _Myt& operator=(_Myt& _Right) throw() //【Must】赋值操作符
        {    // assign compatible _Right (assume pointer)
            reset(_Right.release());
            return (*this);
        }

        _Myt& operator=(auto_ptr_ref<_Ty> _Right) throw()
        {    // assign compatible _Right._Ref (assume pointer)
            _Ty *_Ptr = _Right._Ref;
            _Right._Ref = 0;    // release old
            reset(_Ptr);    // set new
            return (*this);
        }

        ~auto_ptr() //【Must】析构函数
        {    // destroy the object
            delete _Myptr;
        }

        _Ty& operator*() const throw()  // pointer-to-member 操作符
        {    // return designated value
            return (*get());
        }

        _Ty *operator->() const throw() // pointer-to-member 操作符
        {    // return pointer to class object
            return (get());
        }

        _Ty *get() const throw() //获取原始资源
        {    // return wrapped pointer
            return (_Myptr);
        }

        _Ty *release() throw()  //
        {    // return wrapped pointer and give up ownership
            _Ty *_Tmp = _Myptr;
            _Myptr = 0;
            return (_Tmp);
        }

        void reset(_Ty *_Ptr = 0)
        {    // destroy designated object and store new pointer
            if (_Ptr != _Myptr)
                delete _Myptr;
            _Myptr = _Ptr;
        }

    private:
        _Ty *_Myptr;    // the wrapped object pointer
    };
  • 相关阅读:
    [Go] 解决空接口 interface{} cannot use (type []string) as type []interface {}
    [Linux] 脚本中的set -e有什么作用
    [Go] 解决go test 时 testing: warning: no tests to run
    [Go] go for range循环map是无序的 变成有序
    [Linux] ubuntu 32位 i686 安装docker
    [Git] git checkout 恢复未add的修改文件
    [MySQL] in 子查询出现DEPENDENT SUBQUERY问题
    [MySQL] group by 聚合函数的原理和聚合限制原因SELECT list is not in GROUP BY clause and contains nonaggregated column
    [MySQL]mysql的ANY_VALUE()函数 解决 ONLY_FULL_GROUP_BY 模式
    [Go] GODEBUG=inittrace=1 查看所有执行的init函数
  • 原文地址:https://www.cnblogs.com/whyandinside/p/2766421.html
Copyright © 2020-2023  润新知