• 第61课 智能指针类模板


    智能指针的意义:

    STL中的智能指针:

    auto_ptr使用示例:

     1 #include <iostream>
     2 #include <string>
     3 #include <memory>
     4 
     5 using namespace std;
     6 
     7 class Test
     8 {
     9     string m_name;
    10 public:
    11     Test(const char* name)
    12     {
    13         cout << "Hello, " << name << "." << endl;
    14         
    15         m_name = name;
    16     }
    17     
    18     void print()
    19     {
    20         cout << "I'm " << m_name << "." << endl;
    21     }
    22     
    23     ~Test()
    24     {
    25         cout << "Goodbye, " << m_name << "." << endl;
    26     }
    27 };
    28 
    29 int main()
    30 {
    31     auto_ptr<Test> pt(new Test("D.T.Software"));
    32     
    33     cout << "pt = " << pt.get() << endl;
    34     
    35     pt->print();
    36     
    37     cout << endl;
    38     
    39     auto_ptr<Test> pt1(pt);
    40     
    41     cout << "pt = " << pt.get() << endl;
    42     cout << "pt1 = " << pt1.get() << endl;
    43     
    44     pt1->print();
    45     
    46     return 0;
    47 }

    运行结果如下:

    STL中其它的智能指针:

    Qt中的智能指针:

    Qt智能指针示例:

     1 #include <QPointer>
     2 #include <QSharedPointer>
     3 #include <QDebug>
     4 
     5 class Test : public QObject
     6 {
     7     QString m_name;
     8 public:
     9     Test(const char* name)
    10     {
    11         qDebug() << "Hello, " << name << ".";
    12 
    13         m_name = name;
    14     }
    15 
    16     void print()
    17     {
    18         qDebug() << "I'm " << m_name << ".";
    19     }
    20 
    21     ~Test()
    22     {
    23         qDebug() << "Goodbye, " << m_name << ".";
    24     }
    25 };
    26 
    27 int main()
    28 {
    29     QPointer<Test> pt(new Test("D.T.Software"));
    30     QPointer<Test> pt1(pt);
    31     QPointer<Test> pt2(pt);
    32 
    33     pt->print();
    34     pt1->print();
    35     pt2->print();
    36 
    37     delete pt;
    38 
    39     qDebug() << "pt = " << pt;
    40     qDebug() << "pt1 = " << pt1;
    41     qDebug() << "pt2 = " << pt2;
    42 
    43     qDebug() << endl;
    44 
    45     QSharedPointer<Test> spt(new Test("Delphi Tang"));
    46     QSharedPointer<Test> spt1(spt);
    47     QSharedPointer<Test> spt2(spt);
    48 
    49     spt->print();
    50     spt1->print();
    51     spt2->print();
    52 
    53     return 0;
    54 }

    运行结果如下:

    Qt中的其它智能指针:

     智能指针类模板实现:

     1 #ifndef _SMARTPOINTER_H_
     2 #define _SMARTPOINTER_H_
     3 
     4 template
     5 < typename T >
     6 class SmartPointer
     7 {
     8     T* mp;
     9 public:
    10     SmartPointer(T* p = NULL)
    11     {
    12         mp = p;
    13     }
    14     
    15     SmartPointer(const SmartPointer<T>& obj)
    16     {
    17         mp = obj.mp;
    18         const_cast<SmartPointer<T>&>(obj).mp = NULL;
    19     }
    20     
    21     SmartPointer<T>& operator = (const SmartPointer<T>& obj)
    22     {
    23         if( this != &obj )
    24         {
    25             delete mp;
    26             mp = obj.mp;
    27             const_cast<SmartPointer<T>&>(obj).mp = NULL;
    28         }
    29         
    30         return *this;
    31     }
    32     
    33     T* operator -> ()
    34     {
    35         return mp;
    36     }
    37     
    38     T& operator * ()
    39     {
    40         return *mp;
    41     }
    42     
    43     bool isNull()
    44     {
    45         return (mp == NULL);
    46     }
    47     
    48     T* get()
    49     {
    50         return mp;
    51     }
    52     
    53     ~SmartPointer()
    54     {
    55         delete mp;
    56     }
    57 };
    58 
    59 #endif

    测试程序:

     1 #include <iostream>
     2 #include <string>
     3 #include "SmartPointer.h"
     4 
     5 using namespace std;
     6 
     7 class Test
     8 {
     9     string m_name;
    10 public:
    11     Test(const char* name)
    12     {
    13         cout << "Hello, " << name << "." << endl;
    14         
    15         m_name = name;
    16     }
    17     
    18     void print()
    19     {
    20         cout << "I'm " << m_name << "." << endl;
    21     }
    22     
    23     ~Test()
    24     {
    25         cout << "Goodbye, " << m_name << "." << endl;
    26     }
    27 };
    28 
    29 int main()
    30 {
    31     SmartPointer<Test> pt(new Test("D.T.Software"));
    32     
    33     cout << "pt = " << pt.get() << endl;
    34     
    35     pt->print();
    36     
    37     cout << endl;
    38     
    39     SmartPointer<Test> pt1(pt);
    40     
    41     cout << "pt = " << pt.get() << endl;
    42     cout << "pt1 = " << pt1.get() << endl;
    43     
    44     pt1->print();
    45     
    46     return 0;
    47 }

    运行结果如下:

    小结:

  • 相关阅读:
    html图片预览
    网易DBA私享会分享会笔记2
    网易DBA私享会分享会笔记1
    centos6.5适用的国内yum源:网易、搜狐
    如何去除 ckeditor 上传图片后在原码中留下的 style="width: 100%;height:100px"之类的代码呢?
    关于json.ajax ,php的那点事
    去掉所有的html标签
    about JNI
    some knowledge of maven {maven实战}
    What is Proguard?
  • 原文地址:https://www.cnblogs.com/wanmeishenghuo/p/9594385.html
Copyright © 2020-2023  润新知