今天帮着一位大二的学弟写了一个String的类,后来一想这个技术点,也许不是什么难点,但是还是简单的记录一些吧! 为那些还在路上爬行的行者,剖析一些基本的实现.....
内容写的过于简单,没有涉及到其他格式的如考虑utf_8.这儿单纯的考虑了char的使用.......
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<iostream> 3 #include<stdlib.h> 4 #include<string.h> 5 #include<windows.h> 6 using namespace std; 7 8 class MyString { 9 10 public : 11 MyString(const char * str = NULL) ; //默认构造函数 12 MyString(const MyString & mystr); //复制构造函数 13 ~MyString() { 14 if (test != NULL) delete[] test; 15 }; 16 //析构函数 17 int length(); //返回字符串长度 18 void print(); //打印 19 char at(int pos) ; //查找第i个字符 20 MyString & operator = (const MyString &other); //等号操作符重载 21 MyString & operator = (const char * str ); //等号操作符重载 22 private: 23 char * test; 24 }; 25 26 //构造函数 27 MyString::MyString(const char * str) { 28 29 30 if (str == NULL) 31 { 32 test = new char[1]; 33 *test = '