• C++之管理指针成员


     1 #ifndef PLAIN-PTR_H_INCLUDED
     2 #define PLAIN-PTR_H_INCLUDED
     3 //浅复制
     4 class AHasPtr
     5 {
     6 public:
     7     AHasPtr(int *p,int i):ptr(p),val(i){}
     8     //get方法
     9     int *get_ptr() const {return ptr;}
    10     int get_int() const {return val;}
    11     //set方法
    12     void set_ptr(int *p){ptr=p;}
    13     void set_int(int i){val=i;}
    14     //
    15     int get_ptr_val()const {return *ptr;}
    16     void set_ptr_val(int val){*ptr=val;}
    17 private:
    18     int val;
    19     int *ptr;
    20 };
    21 
    22 #endif // PLAIN-PTR_H_INCLUDED
     1 #ifndef VALUE-PTR_H_INCLUDED
     2 #define VALUE-PTR_H_INCLUDED
     3 
     4 //深复制
     5 class CHasPtr
     6 {
     7 public:
     8     //构造函数
     9     CHasPtr(const int &p,int i):ptr(new int(p)),val(i){}
    10     //复制构造函数 CHasPtr p1(p2);
    11     CHasPtr(const CHasPtr &orig):
    12         ptr(new int(*orig.ptr)),
    13         val(orig.val){}
    14     //赋值构造函数 p1=p2
    15     CHasPtr &operator=(const CHasPtr &rhs)
    16     {
    17         //赋值操作符是把指针指向的对象赋值过来
    18         //.的优先级高于*
    19         *ptr=*rhs.ptr;
    20         val=rhs.val;
    21         return *this;
    22     }
    23     //析构函数
    24     ~CHasPtr()
    25     {
    26         delete ptr;
    27     }
    28     //get方法
    29     int *get_ptr() const {return ptr;}
    30     int get_int() const {return val;}
    31     //set方法
    32     void set_ptr(int *p){ptr=p;}
    33     void set_int(int i){val=i;}
    34     //
    35     int get_ptr_val()const {return *ptr;}
    36     void set_ptr_val(int val){*ptr=val;}
    37 private:
    38     int val;
    39     int *ptr;
    40 };
    41 
    42 #endif // VALUE-PTR_H_INCLUDED
     1 #ifndef SMART-PTR_H_INCLUDED
     2 #define SMART-PTR_H_INCLUDED
     3 //智能指针
     4 
     5 class U_Ptr
     6 {
     7     friend class BHasPtr;
     8 private:
     9     int *ip;
    10     //计数 计算有多少个对象使用智能指针
    11     //当计数为0时删除
    12     size_t use;
    13     //当有指针传递进来时就将计数值设为1
    14     U_Ptr(int *p):ip(p),use(1){}
    15     ~U_Ptr()
    16     {
    17         delete ip;
    18     }
    19 };
    20 
    21 class BHasPtr
    22 {
    23 public:
    24     //new U_Ptr(p)将p传递给U_Ptr并赋给ip
    25     BHasPtr(int *p,int i):ptr(new U_Ptr(p)),val(i){}
    26 
    27     BHasPtr(const BHasPtr &orig):
    28         ptr(orig.ptr),
    29         val(orig.val)
    30         {
    31             //复制的时候计数加加,因为有多个使用
    32             ++ptr->use;
    33         }
    34 
    35     BHasPtr &operator=(const BHasPtr &rhs)
    36     {
    37         //??????为什么右边的++左边的--
    38         ++rhs.ptr->use;
    39         if(--ptr->use==0)
    40         {
    41             delete ptr;
    42         }
    43         ptr=rhs.ptr;
    44         val=rhs.val;
    45         return *this;
    46     }
    47 
    48     ~BHasPtr()
    49     {
    50         //如果减到0就删除指针
    51         if(--ptr->use==0)
    52         {
    53             delete ptr;
    54         }
    55     }
    56     //get方法
    57     int *get_ptr() const {return ptr->ip;}
    58     int get_int() const {return val;}
    59     //set方法
    60     void set_ptr(int *p){ptr->ip=p;}
    61     void set_int(int i){val=i;}
    62     //63     //->优先级高于*
    64     int get_ptr_val()const {return *ptr->ip;}
    65     void set_ptr_val(int val){*ptr->ip=val;}
    66 private:
    67     int val;
    68     //智能指针
    69     U_Ptr *ptr;
    70 //    int *ptr;
    71 };
    72 
    73 
    74 #endif // SMART-PTR_H_INCLUDED
     1 #include <iostream>
     2 #include "plain-ptr.h"
     3 #include "value-ptr.h"
     4 #include "smart-ptr.h"
     5 
     6 using namespace std;
     7 
     8 void test_AHasPtr()
     9 {
    10     int i=42;
    11     AHasPtr p1(&i,42);
    12     //浅复制
    13     AHasPtr p2=p1;
    14     cout<<p2.get_ptr_val()<<endl;
    15     p1.set_ptr_val(0);
    16     cout<<p2.get_ptr_val()<<endl;
    17     //申请一抄个整型变量空间,赋初值为42,
    18     //并定义一个整型指针a指向该地百址空间
    19     int *ip=new int(42);
    20     AHasPtr ptr(ip,10);
    21     cout<<ptr.get_ptr_val()<<endl;
    22     delete ip;
    23     cout<<ptr.get_ptr_val()<<endl;
    24 
    25 }
    26 
    27 void test_BHasPtr()
    28 {
    29     int obj=0;
    30     BHasPtr ptr3(&obj,42);
    31     BHasPtr ptr4(ptr3);
    32     cout<<ptr3.get_ptr_val()<<','<<ptr3.get_int()<<endl;
    33     cout<<ptr4.get_ptr_val()<<','<<ptr4.get_int()<<endl;
    34     ptr4.set_ptr_val(2);
    35     ptr4.set_int(22);
    36     cout<<ptr3.get_ptr_val()<<','<<ptr3.get_int()<<endl;
    37     cout<<ptr4.get_ptr_val()<<','<<ptr4.get_int()<<endl;
    38 }
    39 
    40 void test_CHasPtr()
    41 {
    42     int obj=0;
    43     CHasPtr ptr1(obj,42);
    44     CHasPtr ptr2(ptr1);
    45     cout<<ptr1.get_ptr_val()<<','<<ptr1.get_int()<<endl;
    46     cout<<ptr2.get_ptr_val()<<','<<ptr2.get_int()<<endl;
    47     ptr2.set_ptr_val(32);
    48     ptr2.set_int(43);
    49     cout<<ptr1.get_ptr_val()<<','<<ptr1.get_int()<<endl;
    50     cout<<ptr2.get_ptr_val()<<','<<ptr1.get_int()<<endl;
    51 }
    52 
    53 int main()
    54 {
    55     cout<<"=========常规指针 浅复制==========="<<endl;
    56     test_AHasPtr();
    57     cout<<"==========深复制=========="<<endl;
    58     test_CHasPtr();
    59     cout<<"==========智能指针=========="<<endl;
    60     test_BHasPtr();
    61     return 0;
    62 }

     示意图:

  • 相关阅读:
    js this
    python词云的制作方法
    flask表单标签
    scrapy使用PhantomJS爬取数据
    flask连接sqlalchemy数据库,实现简单的登录跳转功能
    useful tools and website
    sqlalchemy精华版
    flask连接数据库mysql+SQLAlchemy
    flask框架get post方式
    flask基础知识
  • 原文地址:https://www.cnblogs.com/yh2924/p/12659392.html
Copyright © 2020-2023  润新知