string類的實現:
#include #include using namespace std;
class string
;//建構函式
string::string()
//建構函式
string::string(const char *str)
//拷貝建構函式
string::string(const string &other)
//=運算子過載函式,返回物件的引用
string &string::operator=(const string &other)
int length = strlen(other.data) + 1;
data = new char[length];
memset(data, 0, length);
strcpy(data, other.data);
return *this;
}//+=運算子過載函式,返回物件引用
string &string::operator+=(const string &other)
//+運算子過載函式,定義為友元函式,返回乙個臨時物件。
string operator+(const string &s1, const string &s2)
//析構函式
string::~string()
//輸出函式
ostream &operator<<(ostream &out, const string &s)
int main()
C String類的實現
參考c primer.string類的實現,清翔兔 06,jan.include using namespace std class string string void private char m data inline string string const char str if str m...
C String類的實現
include using namespace std class string string void private char m data inline string string const char str inline string string const string other i...
c string類的實現
友元函式可以轉換左右運算元的順序,而成員函式必須保證左運算元string已經處於正確的形式。include include includeusing namespace std class string friend const string operator const string other1...