C++面向对象5:智能指针

目录

1.智能指针

2.std::shared_ptr

3.std::unique_ptr

4.模板类


1.智能指针

是一个类对象

get()获得类对象中的指针,获取堆区指针

只管申请,不管释放

2.std::shared_ptr

几个指针指向同一个对象,他们是共享的。这个对象如果没有指针指向,指针就会自动被删除。

构造函数法

std::shared_ptr<MyTime> mt1(new MyTime(10));
std::shared_ptr<MyTime> mt2 = mt1;

shared_ptr<MyTime>他是一个模板类。指定内部元素类型。

mt1变量名

(new MyTime(10));初始化参数,创建一个对象MyTime的构造函数参数是10,作为初始化参数传给Mt1这个对象。

mt2 = mt1;他们指向同一个对象。new MyTime(10)

make_shared函数法

auto mt1 = std::make_shared<MyTime>(1, 70);//和上面申请共享指针一样

make_shared函数的主要功能是在动态内存中分配一个对象并初始化它,返回指向此对象的shared_ptr;由于是通过shared_ptr管理内存,因此一种安全分配和使用动态内存的方法。

#include <iostream>
#include <memory>

class MyTime
{
    int hours;
    int minutes;
  public:
    MyTime(): hours(0), minutes(0)
    {
        std::cout << "Constructor MyTime()" << std::endl;
    }
    MyTime(int m): hours(0), minutes(m)
    {
        std::cout << "Constructor MyTime(int)" << std::endl;
        this->hours +=  this->minutes / 60;
        this->minutes %= 60;
    }
    MyTime(int h, int m): hours(h), minutes(m)
    {
        std::cout << "Constructor MyTime(int,int)" << std::endl;
        this->hours +=  this->minutes / 60;
        this->minutes %= 60;
    }
    ~MyTime()
    {
        std::cout << "Destructor MyTime(). Bye!" << std::endl;
    }
    MyTime operator+(int m) const
    {
        MyTime sum;
        sum.minutes = this->minutes + m;
        sum.hours = this->hours;
        sum.hours +=  sum.minutes / 60;
        sum.minutes %= 60;
        return sum;
    }
    friend std::ostream & operator<<(std::ostream & os, const MyTime & t)
    {
        std::string str = std::to_string(t.hours) + " hours and " 
                        + std::to_string(t.minutes) + " minutes.";
        os << str;
        return os;
    }
};


int main()
{
//1    错误写法
     //std::shared_ptr<MyTime> mt0 = new MyTime(0,70); //error智能指针是一个对象,不是指针
     //MyTime * mt0 = std::make_shared<MyTime>(1, 70); //error不能向普通指针赋值
    {//只有new没有delete
        std::shared_ptr<MyTime> mt1(new MyTime(10));
        std::cout << *mt1 << std::endl;
    }
//2
    {
        std::shared_ptr<MyTime> mt1 = std::make_shared<MyTime>(1, 70);//两个参数的构造函数
        std::shared_ptr<MyTime> mt2 = mt1;
        std::shared_ptr<MyTime> mt3 = mt2;

        std::cout << "mt1: " << *mt1 << std::endl;
        std::cout << "mt2: " << *mt2 << std::endl;
        std::cout << "mt3: " << *mt3 << std::endl;
        std::cout << "use_count() = " << mt2.use_count() << std::endl;//一个对象三个智能指针指向他。

        {
            auto mt4 = mt3;
		//触发了构造函数,在+重载里面sum触发了
            *mt4 = *mt4 + 50;//对智能指针里面含有的对象取内容。
            std::cout << "use_count() = " << mt3.use_count() << std::endl;
        }//mt4生命周期结束
        std::cout << "mt3: " << *mt3 << std::endl;
        std::cout << "use_count() = " << mt3.use_count() << std::endl;
    }//这里结束一切所有的智能指针都被销毁,对象也被销毁了。

    return 0;
}

 

 

3.std::unique_ptr

拥有一个对象之后,他是不允许别人再跟他分享的。

构造函数法

std::unique_ptr<MyTime> mt1(new MyTime(10));//创建一个对象作为指针传给智能指针。

make_unique函数法

std::unique_ptr<MyTime> mt2 = std::make_unique<MyTime>(80); //c++17
//用move拷贝 
std::unique_ptr<MyTime> mt3 = std::move(mt1);
#include <iostream>
#include <memory>

class MyTime
{
    int hours;
    int minutes;
  public:
    MyTime(): hours(0), minutes(0)
    {
        std::cout << "Constructor MyTime()" << std::endl;
    }
    MyTime(int m): hours(0), minutes(m)
    {
        std::cout << "Constructor MyTime(int)" << std::endl;
        this->hours +=  this->minutes / 60;
        this->minutes %= 60;
    }
    MyTime(int h, int m): hours(h), minutes(m)
    {
        std::cout << "Constructor MyTime(int,int)" << std::endl;
        this->hours +=  this->minutes / 60;
        this->minutes %= 60;
    }
    ~MyTime()
    {
        std::cout << "Destructor MyTime(). Bye!" << std::endl;
    }
    MyTime operator+(int m) const
    {
        MyTime sum;
        sum.minutes = this->minutes + m;
        sum.hours = this->hours;
        sum.hours +=  sum.minutes / 60;
        sum.minutes %= 60;
        return sum;
    }
    friend std::ostream & operator<<(std::ostream & os, const MyTime & t)
    {
        std::string str = std::to_string(t.hours) + " hours and " 
                        + std::to_string(t.minutes) + " minutes.";
        os << str;
        return os;
    }
};


int main()
{
    std::unique_ptr<MyTime> mt1(new MyTime(10));
    std::unique_ptr<MyTime> mt2 = std::make_unique<MyTime>(80); //c++17

    std::cout << "mt1: " <<*mt1 << std::endl;
    std::cout << "mt2: " <<*mt2 << std::endl;

    //std::unique_ptr<MyTime> mt3 = mt1; // error不可赋值,因为不共享指针
    std::unique_ptr<MyTime> mt3 = std::move(mt1); 
    //std::shared_ptr<MyTime> mt3 = std::move(mt1);//okay
    
    std::cout << "mt1: " <<*mt1 << std::endl;//指针被置0。
    std::cout << "mt3: " <<*mt3 << std::endl;

    return 0;
}

4.模板类

template< class T > class shared_ptr;//是一个模板类。
std::shared_ptr<MyTime> mt1(new MyTime(10));对象()在调用它的构造函数,这个对象的构造函数去构造这个对象。构造函数的参数是一个MyTime类型指针。
std::shared_ptr<MyTime> mt2 = mt1;//复制构造函数

生命周期结束,对象就会销毁,他会调他的析构函数。 

template<
    class T,
    class Deleter = std::default_delete<T>
> class unique_ptr; //是一个模板类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值