1、String.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #pragma once #include <iostream> using namespace std; class String { friend ostream &operator < < (ostream &, String &); private: char *m_name = NULL; char *setM_name(const char *cstring); char *strAppend(const char *, const char *); public: String(const char *cstring = ""); String(const String &); ~String(); bool operator == (String &string); String &operator = (String &string); String &operator = (const char *cstring); String operator + (const char *cstring); String operator + (const String &string); String &operator += (const char *cstring); String &operator += (const String &string); char operator[](int index); bool operator<(const String &string); }; ostream &operator << (ostream &cout, String &string); |
2、String.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #include "String.h" String::String(const char *name) { operator=(name); } String::String(const String &str) { operator=(str.m_name); } String::~String() { operator=(NULL); } bool String::operator == (String &str) { return m_name == str.m_name; } String &String::operator = (String &str) { return operator=(str.m_name); } String &String::operator = (const char *name) { m_name = setM_name(name); return *this; } String String::operator + (const String &string) { return operator+(string.m_name); } String String::operator + (const char *cstring) { String newString; return newString += cstring; } String &String::operator += (const String &string) { return operator+=(string.m_name); } String &String::operator += (const char *cstring) { char *str = strAppend(m_name, cstring); if (str) { setM_name(NULL); m_name = str; } return *this; } char String::operator[](int index) { if (m_name == NULL || index < 0 || index >= strlen(m_name)) return '\0'; return m_name[index]; } bool String::operator<(const String &string) { int res = strcmp(m_name, string.m_name); return res < 0; } char *String::setM_name(const char *cstring) { if (m_name == cstring) return m_name; if (m_name) { cout << "delete - " << m_name << endl; delete[] m_name; m_name = NULL; } if (cstring) { m_name = new char[strlen(cstring) + 1]; strcpy(m_name, cstring); cout << "new - " << m_name << endl; } return m_name; } char *String::strAppend(const char *str1, const char *str2) { if (str1 == NULL || str2 == NULL) return NULL; char *str = new char[strlen(str1) + strlen(str2) + 1](); strcat(str, str1); strcat(str, str2); cout << "new - " << str << endl; return str; } ostream &operator << (ostream &cout, String &str) { return cout << str.m_name; } |
3、main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> #include "String.h" using namespace std; int main() { { String str1 = "123"; String str2 = "456"; String str3 = str1; str1 = str1; str1 = str2; } system("pause"); return 0; } |
4、执行结果:
new – 123
new – 456
new – 123
delete – 123
new – 456
delete – 123
delete – 456
delete – 456
请按任意键继续. . .