2024/9/8 c++ smart

1.通过自己编写的class来实现unique_ptr指针的功能

#include <iostream>

using namespace std;
template<class T>
class unique_ptr
{
public:
        //无参构造函数
        unique_ptr();
        //有参构造函数
        unique_ptr(T* ptr = nullptr):ptr(ptr){}
        //移动构造函数
        unique_ptr(unique_ptr&& Right):ptr(Right.ptr) {}         
        unique_ptr operator=(unique_ptr<T>&& p)
        {
            ptr=p.release();
        }
 
        //使用delete关键字强行禁止拷贝构造函数和赋值函数的生成
        unique_ptr(const unique_ptr<T>& up) = delete;
        unique_ptr<T>& operator=(const unique_ptr<T>& up) = delete;
         unique_ptr operator=(T*)=delete;//不支持指针赋值

        T* get() const
        {
            return ptr;
        }
        T* release()
        {
            T* tr=ptr;
            ptr=nullptr;
            return tr;
        }

        void reset(T* rt)
        {
            delete ptr;
            ptr=rt;
        }
        void reset()
        {
            delete ptr;
            ptr=nullptr;
        }
    
        template <class U>
        void reset(U p) =delete;

        void swap(unique_ptr& u)
        {
            using std::swap;
            swap(ptr,u.ptr);
        }
        
        
        operator bool() const
        {
            return ptr;
        }

        T& operator[](size_t i) const
        {
            return *(ptr+i);
        }

        T& operator*()
        {
            return *ptr;
        }
 
        T* operator->()
        {
            return ptr;
        }

        //析构函数
        ~unique_ptr()
        {
            delete ptr;
        }
 
    private:
        T*     ptr;
    };


class Test
{
    public:
        Test(){};   //使用系统默认提供的无参构造函数
        Test(int a):a(a){
            cout << "Test的有参构造函数" << endl;
        }

        virtual ~Test()
        {
            cout << "Test的析构函数" << endl;
        }

        void show()
        {
            cout << "Test::a=" << a << endl;
        }
                                                                                           
    private:
        int a;
};

int main(int argc, const char *argv[])
{
    Test* pt1 = new Test(10);       //pt1是裸指针

    unique_ptr<Test> up(pt1);   //将裸指针pt1交给up智能指针维护
    up->show();
    (*up).show();

    //unique_ptr<Test> up2(up);     //调用拷贝构造函数,错误的,该函数已经被禁用

    //unique_ptr<Test> up2;
    //up2 = up;                         //调用拷贝赋值函数,错误的,该函数已经被禁用

    unique_ptr<Test> up2(move(up));     //调用移动构造函数,move函数将up从左值修改为右值
                                        //将up中的数据移动给up2,自己空间中的指针置为NULL

    //up->show();       //错误的,up已经将自己的数据移交给up2了
                        //up中的裸指针权限已经移交给up2

    up2->show();        //up2->show():通过pt1访问类pt1的show


    Test* pt2 = new Test(20);
    unique_ptr<Test> up3(pt2);      //up3维护的是pt2裸指针
    up3->show();                    //通过pt2访问了pt2的show

    up3.swap(up2);                  //将up2的裸指针与up3的裸指针交换

    up2->show();        //20    通过pt2访问show
    up3->show();        //10    通过pt1访问show
    //因为up将他的值给了up2,此时up经过move值不确定所以会出现段核心报错
    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值