gavanwanggw

导航

管理指针成员(智能指针)

C++ Primer 学习中。。。

 

简单记录下我的学习过程 (代码为主) 


【题外话】过几天就要出去找工作了。这几天在家看看曾经做过的题。

。。如今想想时间过得真的好快,希望自己能找一份自己惬意的工作。

。。!!

奋斗   <梦想还是要有的,万一实现了呢!

>


以下是学习心得:



这幅图非常好的阐述了仅仅能指针的概念,事实上智能指针就是一个计数类!以后多用用就熟悉了。





plain-ptr.h


#ifndef PLAIN-PTR_H_INCLUDED
#define PLAIN-PTR_H_INCLUDED

class AHasPtr{
public:
    AHasPtr(int *p,int i):ptr(p),val(i){}
    //设计接口
    int *get_ptr() const {return ptr;}
    int get_int() const {return val;}

    void set_ptr(int *p) {ptr=p;}
    void set_int(int i) {val=i;}

    int get_ptr_val()const {return *ptr;}
    void set_ptr_val(int val)const{*ptr=val;}
private:
    int val;
    int *ptr;   //有指针成员,一般都是浅复制
};

#endif // PLAIN-PTR_H_INCLUDED


smart-ptr.h

#ifndef SMART-PTR_H_INCLUDED
#define SMART-PTR_H_INCLUDED

class U_Ptr
{
    friend class BHasPtr;
private:
    int *ip;
    size_t use; //计数
    U_Ptr(int *p):ip(p),use(1){}
    ~U_Ptr(){delete ip;}
};

class BHasPtr{
public:
    BHasPtr(int *p,int i):ptr(new U_Ptr(p)),val(i){}
    BHasPtr(const BHasPtr &orig):ptr(orig.ptr),val(orig.val)//复制构造函数
    {
        ++ptr->use;
    }
    BHasPtr& operator=(const BHasPtr&);

    ~BHasPtr()
    {
        if(--ptr->use == 0) delete ptr;
    }
    //设计接口
    int *get_ptr() const {return ptr->ip;}
    int get_int() const {return val;}

    void set_ptr(int *p) {ptr->ip=p;}
    void set_int(int i) {val=i;}

    int get_ptr_val()const {return *ptr->ip;}
    void set_ptr_val(int val)const{*ptr->ip=val;}
private:
    int val;
//    int *ptr;
    U_Ptr *ptr; //这里使用智能指针类
};

BHasPtr& BHasPtr::operator=(const BHasPtr &rhs)
{
    ++rhs.ptr->use;
    if(--ptr->use == 0)delete ptr;

    ptr = rhs.ptr;
    val = rhs.val;
    return *this;
}
#endif // SMART-PTR_H_INCLUDED


value-ptr.h

#ifndef VALUE-PTR_H_INCLUDED
#define VALUE-PTR_H_INCLUDED

class CHasPtr{
public:
    CHasPtr(const int &p,int i):ptr(new int(p)),val(i){}
    CHasPtr(const CHasPtr &orig)
        :ptr(new int(*orig.ptr)),val(orig.val){}
    CHasPtr& operator=(const CHasPtr&);

    ~CHasPtr(){delete ptr;}//三原则:一起写复制函数、=操作符函数和析构函数
    //设计接口
    int *get_ptr() const {return ptr;}
    int get_int() const {return val;}

    void set_ptr(int *p) {ptr=p;}
    void set_int(int i) {val=i;}

    int get_ptr_val()const {return *ptr;}
    void set_ptr_val(int val)const{*ptr=val;}
private:
    int val;
    int *ptr;   //有指针成员。一般都是浅复制
};
CHasPtr& CHasPtr::operator = (const CHasPtr &rhs)
{
    *ptr=*rhs.ptr;
    val=rhs.val;
    return *this;
}

#endif // VALUE-PTR_H_INCLUDED


main函数

#include <iostream>
#include "plain-ptr.h"
#include "smart-ptr.h"
#include "value-ptr.h"

using namespace std;
void test_AHasPtr()//浅拷贝&&悬垂指针
{
    int i=42;
    AHasPtr p1(&i,50);
    AHasPtr p2=p1;//浅拷贝
    cout<<"常规指针类(浅拷贝):"<<endl<<"原始数据:"<<endl;
//    cout<<p2.get_ptr_val()<<endl;
    cout<< p1.get_ptr_val()<<","<<p1.get_int()<<endl;
    cout<< p2.get_ptr_val()<<","<<p2.get_int()<<endl;

    p1.set_ptr_val(0);//改动p1
    cout<<"改动以后:"<<endl;
//    cout<<p2.get_ptr_val()<<endl;
    cout<< p1.get_ptr_val()<<","<<p1.get_int()<<endl;
    cout<< p2.get_ptr_val()<<","<<p2.get_int()<<endl;

    /**---以下来看野指针的情况---**/
    int *id = new int(66);
    AHasPtr ptr(id,10);
    cout<<ptr.get_ptr_val()<<endl;//66
    //删除id指针
    delete id;
  //  cout<<ptr.get_ptr_val()<<endl;//输出乱码
    //由于指向的id指针已经被删除了,就成了野指针or悬垂指针
    cout<<endl;
}

void test_CHasPtr()
{
    int obj = 0;
    CHasPtr p1(obj,12);
    CHasPtr p2(p1);
    cout<<"值型类(深拷贝):"<<endl<<"原始数据:"<<endl;
    cout<< p1.get_ptr_val()<<","<<p1.get_int()<<endl;
    cout<< p2.get_ptr_val()<<","<<p2.get_int()<<endl;

    p2.set_ptr_val(6);
    p2.set_int(24);
    cout<<"改动以后:"<<endl;
    cout<< p1.get_ptr_val()<<","<<p1.get_int()<<endl;
    cout<< p2.get_ptr_val()<<","<<p2.get_int()<<endl;
    cout<<endl;
}

void test_BHasPtr()
{
    int obj = 75;
    BHasPtr p1(&obj,88);
    BHasPtr p2(p1);
    cout<<"智能指针类(计数类):"<<endl<<"原始数据:"<<endl;
    cout<< p1.get_ptr_val()<<","<<p1.get_int()<<endl;
    cout<< p2.get_ptr_val()<<","<<p2.get_int()<<endl;

    p2.set_ptr_val(15);
    p2.set_int(22);
    cout<<"改动以后:"<<endl;
    cout<< p1.get_ptr_val()<<","<<p1.get_int()<<endl;
    cout<< p2.get_ptr_val()<<","<<p2.get_int()<<endl;
    cout<<endl;
}
int main()
{
    test_AHasPtr();
    test_CHasPtr();
    test_BHasPtr();
    cout << "Hello world!" << endl;
    return 0;
}



Output:


常规指针类(浅拷贝):
原始数据:
42,50
42,50
改动以后:
0,50
0,50
66

值型类(深拷贝):
原始数据:
0,12
0,12
改动以后:
0,12
6,24

智能指针类(计数类):
原始数据:
75,88
75,88
改动以后:
15,88
15,22

Hello world!



posted on 2017-08-02 20:36  gavanwanggw  阅读(293)  评论(0编辑  收藏  举报