乙個String類的建簡易實現

2021-10-07 07:04:12 字數 1257 閱讀 9289

其中比較重要的那幾個建構函式和析構函式;如果能夠把ostream的過載寫出來將會更加流弊

class

string

public

:string

(const

char

* str =

null);

//建構函式 加分項:以下內容加const很重要

string

(const string& other)

;//拷貝構造

~string

(void);

//析構

string&

operator=(

const string& other)

;//賦值函式

char

*operator*(

);private

:char

* m_data;

ostream&

print

(ostream& out)

const;}

;string::

string

(const

char

* str)

else

}string::

~string

(void

)string::

string

(const string& other)

string& string::

operator=(

const string& other)

if(m_data)

delete

m_data;

//得分點:刪除原來的記憶體資源 這兒沒有用到引用計數,所以可以直接delete掉

int length =

strlen

(other.m_data)

; m_data =

newchar

[length +1]

;//如果能給m_data加乙個null判斷更好

strcpy

(m_data, other.m_data)

;return

*this

;//返回本物件的引用

}char

* string::

operator*(

)ostream& string::

print

(ostream& out)

const

實現乙個string類

需要實現的基本功能 建構函式 拷貝建構函式 賦值函式 析構函式.以前合稱big three,現在叫做copy control 1 class string 1213 不簡潔版本 14string string const char str else 23 24 2526 string string ...

簡易String類的實現

ifndef mystring h define mystring h include include include 本質是對mystring的包裝 class mystring endif原始檔 define crt secure no warnings include mystring.h m...

簡易string類的實現

在面試的時候,經常會讓寫乙個字串類,看似很簡單的問題,其實經常出錯。閒來沒事,就寫個簡單字串類吧。實現容量的概念,size和capacity 構造 析構 拷貝構造 過載 運算子 swap交換函式,push back,pop函式 擴容函式 右值引用 高效的swap交換 空字串類的形式 擴容實現 必須使...