溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C++深淺拷貝和string類的寫法有哪些

發(fā)布時間:2022-03-10 12:30:30 來源:億速云 閱讀:148 作者:小新 欄目:開發(fā)技術

小編給大家分享一下C++深淺拷貝和string類的寫法有哪些,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

    一、深淺拷貝

    拷貝這個詞對于我們來說應該不陌生,比如我們平常的復制和粘貼就是拷貝;但是如果把拷貝這個詞放到C++中來說就有一些復雜了,我們先來看一下什么是淺拷貝:

    下面用字符串類來模擬實現。

    class Astring
    {
    public:
    	//構造函數
    	Astring(const char* str = "")
    	{
    		_str = new char[strlen(str) + 1];
    		strcpy(_str, str);
    	}
    	//采用淺拷貝寫的構造函數
    	Astring(const Astring& s)
    	{
    		_str = s._str;
    	}
    	//析構函數
    	~Astring()
    	{
    		delete[] _str;
    		_str = nullptr;
    	}
    private:
    	char* _str;
    };
    int main()
    {
    	Astring aa("hello C++");
    	Astring bb(aa); //這里調用拷貝構造
    	return 0;
    }

    當我們執(zhí)行以上程序的話就會失敗,結果如下:

    C++深淺拷貝和string類的寫法有哪些

    分析如下圖所示:

    C++深淺拷貝和string類的寫法有哪些

    所以我們采用淺拷貝使用同一塊空間是不行了,那么怎么辦呢?當然是重新開一塊和別人同樣大小的空間,然后再把別人空間里面的內容給拷貝過來,而這樣就是所謂的深拷貝了;我們還是用字符串類來模擬實現深拷貝:

    class Astring
    {
    public:
    	//構造函數
    	Astring(const char* str = "")
    	{
    		_str = new char[strlen(str) + 1];
    		strcpy(_str, str);
    	}
    	//采用深拷貝寫的構造函數
    	Astring(const Astring& s)
    	{
    		_str = new char[strlen(s._str) + 1];
    		strcpy(_str, s._str);
    	}
    	//析構函數
    	~Astring()
    	{
    		delete[] _str;
    		_str = nullptr;
    	}
    private:
    	char* _str;
    };
    
    int main()
    {
    	Astring aa("hello C++");
    	Astring bb(aa);
    	return 0;
    }

    分析如下圖所示:

    C++深淺拷貝和string類的寫法有哪些

    二、string類的兩種寫法

    有了上面我們知道的深淺拷貝,所以我們明白類中的拷貝構造函數和賦值重載一定要用深拷貝來實現,不過拷貝構造函數和賦值重載還是有兩種寫法的。

    1. 傳統(tǒng)寫法

    傳統(tǒng)寫法就是要自己開辟空間自己來拷貝別人的東西,什么事情都要自己干,代碼如下:

    //搞一個命名空間,里面實現自己寫的string類
    namespace cjy
    {
    	class string
    	{
    	public:
    		//構造函數
    		string(const char* str = "")
    			:_str(new char[strlen(str) + 1])
    		{
    			strcpy(_str, str);
    		}
    		//拷貝構造函數
    		string(string& s)
    			:_str(new char[strlen(s._str) + 1])
    		{
    			strcpy(_str, s._str);
    		}
    		//賦值重載,s1=s3
    		string& operator=(const string& s)
    		{
    			if (this != &s)
    			{
    				char* tmp = new char[strlen(s._str) + 1];
    				delete[] _str;
    				_str = tmp;
    				strcpy(_str, s._str);
    			}
    			return *this;
    		}
    		//析構函數
    		~string()
    		{
    			delete[] _str;
    			_str = nullptr;
    		}
    	private:
    		char* _str;
    	};
    }

    2. 現代寫法

    現代寫法就是復用其它的函數,自己不用干活,交給其它函數來幫你實現,代碼如下:

    //現代寫法:拷貝構造、賦值重載函數
    namespace cjy
    {
    	class string
    	{
    	public:
    		//構造函數
    		string(const char* str = "")
    		{
    			_str = new char[strlen(str) + 1];
    			strcpy(_str, str);
    		}
    		//拷貝構造函數
    		string(const string& s)
    			:_str(nullptr)
    		{
    			string tmp(s._str);
    			std::swap(_str, tmp._str);
         	}
    		//賦值重載
    		string& operator=(string s)
    		{
    			std::swap(_str, s._str);
    			return *this;
    		}
    	private:
    		char* _str;
    	};
    }

    以上是“C++深淺拷貝和string類的寫法有哪些”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

    向AI問一下細節(jié)

    免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

    AI