c++淺拷貝以及淺拷貝的解決方案

淺拷貝:以string類爲例
c++淺拷貝以及淺拷貝的解決方案 當對一個已知對象進行拷貝時,編譯系統會自動調用一種構造函數 —— 拷貝構造函數,如果用戶未定義拷貝構造函數,則會調用默認拷貝構造函數。默認拷貝構造屬於淺拷貝,相當於兩個指針變量指向了同一塊地址空間,調用析構函數時,會delete兩次,所以在第二次delete時會發生中斷(無法尋址地址)

//淺拷貝
class string
    {
    private:
        char* _str;
    public:
        string(char* str = "")//構造函數
        {
            if (nullptr == str)
            {
                str = "";
            }
            _str = new char[strlen(str) + 1];
            strcpy(_str,str);
        }
        string(const string& s)//拷貝構造
            :_str(s._str)
        {
        }
        string operator =(string& s)//賦值構造
        {
            _str = s._str;
            return *this;
        }
        ~string()
        {
            if (_str)
            {
                delete _str;
                _str = nullptr;
            }
        }
    };

解決方案1.傳統深拷貝

//缺點:需要多次開闢空間,代碼冗餘
//優點:可讀性高
class string
{
private:
    char* _str;
public:
    string(char* str = "")
    {
        if (str == nullptr)
        {
            str = "";
        }
        _str = new char[strlen(str) + 1];
        strcpy(_str,str);
    }
    string(const string& s)
        :_str(new char[strlen(s._str)+1])
    {
        strcpy(_str,s._str);
    }
    string& operator=(string& s)
    {
        if (this != &s)
        {
            char* temp = new char[strlen(s._str) + 1];
            strcpy(temp,s._str);
            delete _str;
            _str = temp;
        }
        return *this;
    }
    ~string()
    {
        if (_str)
        {
            delete _str;
            _str = nullptr;
        }
    }
};

解決方案2.

//優點:代碼高效    缺點:可讀性不高
class string
    {
    private:
        char* _str;
    public:
        string(char* str="")
        {
            if (str == nullptr)
            {
                str = "";
            }
            _str = new char[strlen(str) + 1];
            strcpy(_str,str);
        }
        string(const string& s)
            :_str(nullptr)
        {
            string temp(s._str);
            swap(_str,temp._str);
        }
        /*string& operator=(const string& s)
        {
            if (this != &s)
            {
                string temp = s._str;
                swap(_str,temp._str);
            }
            return *this;
        }*/
        string& operator=(string s)//直接改變指向(傳值:臨時變量)
        {
            swap(_str,s._str);
            return *this;
        }
        ~string()
        {
            if (_str)
            {
                delete[] _str;
                _str = nullptr;
            }
        }
    };

解決方案3.淺拷貝+計數

class string
    {
    private :
        char* _str;
        int* _count;
    public :
        string(char* str="")
            : _count(new int(1))//調用普通構造:_count初始化爲1
            , _str(new char[strlen(str) + 1])
        {
            if (str == nullptr)
            {
                str = "";
            }

            strcpy(_str,str);
        }
        string(const string& s)
            :_str(s._str)
            , _count(s._count)
        {
            ++(*_count);
        }
        string operator=(string& s)
        {
            if (this != &s)
            {
                if (0 == --(*_count))
                {
                    delete[] _str;
                    delete _count;
                    _str = nullptr;
                    _count = nullptr;
                }
                _str = s._str;
                _count = s._count;
                ++(*_count);
            }
            return *this;
        }
        ~string()
        {
            if (_str&&0 == --(*_count))
            {
                delete[] _str;
                delete _count;
                _str = nullptr;
                _count = nullptr;
            }
        }
        char& operator[](size_t index)
        {
            if ((*_count) > 1)
            {
                string temp(_str);
                this->swap(temp);
            }
            return _str[index];
        }
        void swap(string& s)
        {
            std::swap(_str,s._str);
            std::swap(_count,s._count);
        }
        const char& operator[](size_t index)const
        {
            return _str[index];
        }
    };
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章