码迷,mamicode.com
首页 > 其他好文 > 详细

引用计数——深拷贝&浅拷贝

时间:2019-04-07 16:55:42      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:注意   null   clu   调试   upper   char   技术   span   data   

 

技术图片

 

下面是用代码实现:

private:
    char *data;
    size_t use_count;
public:
    //构造函数
    String_rep(const char *str = ""):use_count(0)
    {
        if(str == NULL)
        {
            data = new char[1];
            data[0] = \0;
        }
        else
        {
            data = new char[strlen(str)+1];
            strcpy(data,str);
        }
    }
    //拷贝构造函数
    String_rep(const String_rep &rep);
    //析构函数
    ~String_rep()
    {
        if(use_count == 0)
        {
            delete[] data;
            data = NULL;
        }
    }

private:
    String_rep *rep;
public:
    String(const char *str = ""):rep(new String_rep(str))
    {
        rep->increment();
    }
    String(const String &s)
    {
        rep = s.rep;
        rep->increment();
    }
    ~String()
    {
        rep->decrement();
    }

注意理解下面这一段:(调试记录图)

  //析构函数
    ~String_rep()
    {
        if(use_count == 0)
        {
            delete[] data;
            data = NULL;
        }
    }

技术图片

 

还要注意一点,如果要修改其中的内容的话就得先开一块空间,把值拷贝过来然后,再在里面进行修改。

eg:

void upper()
    {
        rep->decrement();
        rep = new String_rep(rep->data);
        
        int n = strlen(rep->data);
        for(int i=0;i<n;++i)
        {
            if(rep->data[i] >=a&&rep->data[i]<=z)
                rep->data[i] = rep->data[i] - 32;
        }
        rep->increment();
 
    }

 

以String类的实现来做个示例:

#pragma once
#include<iostream>
using namespace std;
 
class String;
ostream& operator<<(ostream &out, const String &s);
void Turn(String &s);
 
class String_rep
{
    friend void Turn(String &s);
    friend class String;
    friend ostream& operator<<(ostream &out, const String &s);
private:
    char *data;
    size_t use_count;
public:
    //构造函数
    String_rep(const char *str = ""):use_count(0)
    {
        if(str == NULL)
        {
            data = new char[1];
            data[0] = \0;
        }
        else
        {
            data = new char[strlen(str)+1];
            strcpy(data,str);
        }
    }
    //拷贝构造函数
    String_rep(const String_rep &rep);
    //析构函数
    ~String_rep()
    {
        if(use_count == 0)
        {
            delete[] data;
            data = NULL;
        }
    }
public:
    void increment()
    {
        ++use_count;
    }
    void decrement()
    {
        --use_count;
    }
};
class String
{
    friend void Turn(String &s);
    friend ostream& operator<<(ostream &out, const String &s);
private:
    String_rep *rep;
public:
    String(const char *str = ""):rep(new String_rep(str))
    {
        rep->increment();
    }
    String(const String &s)
    {
        rep = s.rep;
        rep->increment();
    }
    ~String()
    {
        rep->decrement();
    }
    String &operator = (const String &s)
    {
        if(this !=&s)
        {
            rep->decrement();
            rep = s.rep;
            rep->increment();
        }
        return *this;
    }
    String &operator+(const String &s1)
    {
        char *tempch;
        int n = strlen(rep->data)+strlen(s1.rep->data)+1;
        tempch = new char[n];
        strcpy(tempch,rep->data);
        strcat(tempch,s1.rep->data);
        String *temp;
        temp = new String(tempch);
        temp->rep->decrement();
        return *temp;
    }
    String &operator+=(const String &s1)
    {
        char *tempch;
        int n = strlen(rep->data)+strlen(s1.rep->data)+1;
        tempch = new char[n];
        strcpy(tempch,rep->data);
        strcat(tempch,s1.rep->data);
 
        String_rep *temp;
        temp = new String_rep(tempch);
        int remsize = rep->use_count;
        rep = temp;
        rep->use_count = remsize;
        delete tempch;
        return *this;         
    }
    void upper()
    {
        rep->decrement();
        rep = new String_rep(rep->data);
        
        int n = strlen(rep->data);
        for(int i=0;i<n;++i)
        {
            if(rep->data[i] >=a&&rep->data[i]<=z)
                rep->data[i] = rep->data[i] - 32;
        }
        rep->increment();
 
    }
};
ostream& operator<<(ostream &out, const String &s)
{
    out<<s.rep->data;
    return out;
}

使用轮子:

#include"use_count_string.h"
int main()
{
    String st1("asdfghj");
    String st2(st1);
    String st6(st1);
    String st7(st1);
    String st3("qwerty");
    String st5("Anna");
    String st4;
    st4 = st1 + st3;
    st5 += st3; 
    st1 = st3;
    cout<<"st1 = "<<st1<<endl;
    cout<<"st2 = "<<st2<<endl;
    cout<<"st3 = "<<st3<<endl;
    cout<<"st4 = "<<st4<<endl;
    cout<<"st5 = "<<st5<<endl;
    st2.upper();
    cout<<"st2 = "<<st2<<endl;
    return 0;
}

 

引用计数——深拷贝&浅拷贝

标签:注意   null   clu   调试   upper   char   技术   span   data   

原文地址:https://www.cnblogs.com/cjn123/p/10665930.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!