C++中的智能指针

2017-10-14  本文已影响0人  nethanhan

就像这样:

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

举个例子:

#include <iostream>
#include <string>

using namespace std;

class Test
{
    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;
}

智能指针是现代C++开发库中最重要的类模板之一,也是C++中自动内存管理的主要手段,能够在很大程度上避开内存相关的问题。

STL中的智能指针auto_ptr

示例:

#include <iostream>
#include <string>
#include <memory>

using namespace std;

class Test
{
    string m_name;
public:
    Test(const char* name)
    {
        cout << "Hello, " << name << "." << endl;
        
        m_name = name;
    }
    
    void print()
    {
        cout << "I'm " << m_name << "." << endl;
    }
    
    ~Test()
    {
        cout << "Goodbye, " << m_name << "." << endl;
    }
};

int main()
{
    //使用智能指针指向一个类
    auto_ptr<Test> pt(new Test("D.T.Software"));
    //使用智能指针执行成员函数
    cout << "pt = " << pt.get() << endl;
    pt->print();
    cout << endl;
    
    //新定义一个智能指针,用原pt指针赋值
    auto_ptr<Test> pt1(pt);
    //原pt指针赋值后为空
    cout << "pt = " << pt.get() << endl;
    cout << "pt1 = " << pt1.get() << endl;
    
    pt1->print();
    
    return 0;
}

输出结果为:

Hello, D.T.Software.
pt = 0x7fb5ab402680
I'm D.T.Software.

pt = 0x0
pt1 = 0x7fb5ab402680
I'm D.T.Software.
Goodbye, D.T.Software.

STL中的其它智能指针

在最后来演示一个用类模板来模仿指针智能的实例:

#ifndef _SMARTPOINTER_H_
#define _SMARTPOINTER_H_

template
< typename T >
class SmartPointer
{
    T* mp;
public:
    //构造函数
    SmartPointer(T* p = NULL)
    {
        mp = p;
    }
    //拷贝构造函数,当另一个智能指针类赋值时
    SmartPointer(const SmartPointer<T>& obj)
    {
        mp = obj.mp;
        const_cast<SmartPointer<T>&>(obj).mp = NULL;
    }
    //当另一个智能指针类赋值时
    SmartPointer<T>& operator = (const SmartPointer<T>& obj)
    {
        if( this != &obj )
        {
            delete mp;
            mp = obj.mp;
            const_cast<SmartPointer<T>&>(obj).mp = NULL;
        }
        
        return *this;
    }
    //重载 -> 和 * 操作符
    T* operator -> ()
    {
        return mp;
    }
    
    T& operator * ()
    {
        return *mp;
    }
    //判断是否为空
    bool isNull()
    {
        return (mp == NULL);
    }
    //获取指针
    T* get()
    {
        return mp;
    }
    //析构
    ~SmartPointer()
    {
        delete mp;
    }
};

#endif

使用函数(test.cpp):

#include <iostream>
#include <string>
#include "SmartPointer.h"

using namespace std;

class Test
{
    string m_name;
public:
    Test(const char* name)
    {
        cout << "Hello, " << name << "." << endl;
        
        m_name = name;
    }
    
    void print()
    {
        cout << "I'm " << m_name << "." << endl;
    }
    
    ~Test()
    {
        cout << "Goodbye, " << m_name << "." << endl;
    }
};

int main()
{
    //创建一个智能指针并指向test类
    //使用智能指针来执行成员函数
    SmartPointer<Test> pt(new Test("D.T.Software"));
    cout << "pt = " << pt.get() << endl;
    pt->print();
    
    cout << endl;
    
    //给另一个指针指针赋值
    SmartPointer<Test> pt1(pt);
    
    cout << "pt = " << pt.get() << endl;
    cout << "pt1 = " << pt1.get() << endl;
    
    pt1->print();
    
    return 0;
}

运行结果为:

Hello, D.T.Software.
pt = 0x7fd773402680
I'm D.T.Software.

pt = 0x0
pt1 = 0x7fd773402680
I'm D.T.Software.
Goodbye, D.T.Software.
上一篇 下一篇

猜你喜欢

热点阅读