智能指针分析

智能指针分析

1、永恒的话题

  • 内存泄漏(臭名昭著的Bug)
    • 动态申请堆空间,用完后不归还
    • C++语言中没有垃圾回收的机制
    • 指针无法控制所指堆空间的生命周期

2、示例程序

#include<iostream>
#include<string>

using namespace std;

class Test
{
private:
    int i;
public:
    Test(int i)
    {
        this->i = i;
    }
    int value()
    {
        return i;
    }
    ~Test()
    {

    }
};


int main()
{
    for(int i = 0; i < 5; i++)
    {
        Test* p = new Test(i);
        cout << p->value() << endl;
    }
    return 0;
}

3、深度的思考

  • 我们需要什么?
    • 需要一个特殊的指针
    • 指针生命周倜结束时主动释放堆空间
    • 一片堆空间最多只能由一个指针标识
    • 杜绝指针运算和指针比较

4、解决方案

  • 重载指针特征操作符(->*
  • 只能通过类的成员函数重载
  • 重载函数不能使用参数
  • 只能定义一个重载函数

5、编程实验

//37-2.cpp
#include<iostream>
#include<string>

using namespace std;

class Test
{
private:
    int i;
public:
    Test(int i)
    {
        cout << "Test(int i)" << endl;
        this->i = i;
    }
    int value()
    {
        return i;
    }
    ~Test()
    {
        cout << "~Test()" << endl;
    }
};

class Pointer
{
    Test* mp;
public:
    Pointer(Test* p = NULL)
    {
        mp = p;
    }
    Test* operator -> ()
    {
        return mp;
    }
    Test& operator * ()
    {
        return *mp;
    }
    ~Pointer()
    {
        delete mp;
    }
};

int main()
{
    for(int i = 0; i < 5; i++)
    {
        Pointer p = new Test(i);
        cout << p->value() << endl;

    }
    return 0;
}
//compile;run

$ ./a.out 
Test(int i)
0
~Test()
Test(int i)
1
~Test()
Test(int i)
2
~Test()
Test(int i)
3
~Test()
Test(int i)
4
~Test()


还没懂,智能指针的生命周期时间?

//37-3.cpp

#include<iostream>
#include<string>

using namespace std;

class Test
{
private:
    int i;
public:
    Test(int i)
    {
        cout << "Test(int i)" << endl;
        this->i = i;
    }
    int value()
    {
        return i;
    }
    ~Test()
    {
        cout << "~Test()" << endl;
    }
};

class Pointer
{
    Test* mp;
public:
    Pointer(Test* p = NULL)
    {
        mp = p;
    }
    Pointer(const Pointer& obj)
    {
        mp = obj.mp;
        const_cast<Pointer&>(obj).mp = NULL;
    }
    Pointer& operator = (const Pointer& obj)
    {
        if(this != &obj)
        {
            delete mp;
            mp = obj.mp;
            const_cast<Pointer&>(obj).mp = NULL;
        }

        return *this;
    }
    Test* operator -> ()
    {
        return mp;
    }
    Test& operator * ()
    {
        return *mp;
    }
    bool isNull()
    {
        return (mp == NULL);
    }
    ~Pointer()
    {
        delete mp;
    }
};

int main()
{
    Pointer p1 = new Test(0);

    cout << p1->value() << endl;

    Pointer p2 = p1;

    cout << p1.isNull() << endl;

    cout << p2->value() << endl;
    
    return 0;
}


6、智能指针的使用军规

只能用来指向堆空间中的对象或者变量

7、小结

  • 指针特征操作符(->*)可以被重载
  • 重载指针特征符能够使用对象代替指针
  • 智能指针只能用于指向堆空间中的内存
  • 智能指针的意义在于最大程度的规避内存问题
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值