String字串類實現

2021-08-10 22:53:46 字數 1196 閱讀 5975

1、

class

string

;

2、建構函式
string::string(const char *str)

else

}

string str("hell0");
3、析構函式
string::~string()

4、拷貝構造
string::string(const

string &other)

string s1("hello");

string s2=s1;

5、拷貝賦值
string& string::operator=(const string &other)

string s1("hello");

string s2;

s2 = s1;

6、移動構造
string(string&& other):m_data(nullptr)

//或者借用move()

string(string&& other):m_data(std::move(other.m_data))

7、移動賦值
string& operator = (string&& other)

return *this;

}//或者借用move()

string& operator = (string&& other)

8、賦值操作符的
如果不加const的話:

string s3("pello");

const

string s4("qello");

s3 = s4;

這樣就會,因為乙個 const 變數不能隨意轉化成非const變數

另外,string s7("pello");

string s8("pellp");

string s9("qellp");

s9 = s7+s8;

不用const 會報錯,因為 + 賦值必須返回乙個操作值已知的string物件 ,除非他是乙個const物件

c 字串類String的實現

include class string string string strobj string const char tostring int getlength string operator string strobj string operator char s string operato...

字串類string的基本實現

包含指標成員的類深賦值運算子過載,類設計,類的預設 拷貝建構函式,析構函式 實驗參考 class string 預設建構函式 string string 自定義建構函式 string string const char str else 拷貝建構函式 string string const stri...

實現自己的字串類String

因為string預分配與sds上比較有些不足,所以重新封裝了自己的字串類string。fdb string.h fdb string.h fdb中的字串類。以有文件,見compare string sds.md new的使用仍有瓶頸。且存在new的遺留問題。class string 友元函式 1.f...