首页 > 代码库 > 自己写的String类
自己写的String类
1:MyString.h
#pragma onceclass myCString{public: int GetLength()const {return m_nLength;} myCString(void);//无参构造函数 myCString(const char* p);//根据字符串常量初始化 myCString(const myCString& rhs);//拷贝构造 myCString(char ch,int nRepeat = 1);//串内有n个ch字符 myCString(const char* cstr, int nLength);//根据字符串常量的一部分进行构造 operator const char* (){return m_pDate;}//类型转换函数public: char operator[](int nIndex) const;//获取串内上的某个字符 myCString& operator = (const char* cstr); myCString& operator = (const myCString& str); myCString& operator +=(const char* p); myCString& operator +=(const myCString& str);public: myCString operator+( const char* pszSrc) const; //来源不改变,返回一个对象 myCString operator+( const myCString& str) const; //来源不改变,返回一个对象 friend bool operator!=(const myCString& str1,const myCString& str2); friend bool operator==(const myCString& str1,const myCString& str2); friend bool operator< (const myCString& str1, const myCString& str2); friend bool operator> (const myCString& str1, const myCString& str2); ~myCString(void);private: int m_nLength; char* m_pDate;};
MyString.cpp
#include "stdafx.h"#include "myCString.h"#include <string.h>#include <iostream>using namespace std;myCString::myCString(void){ m_pDate = new char(0); m_nLength = 0;}myCString::myCString(const char* p){ if (p == NULL) { m_pDate = new char[1]; *m_pDate = '\0'; } m_nLength = strlen(p); m_pDate = new char[m_nLength+1]; strcpy(m_pDate,p);}myCString::myCString(const myCString& rhs){ m_nLength = rhs.m_nLength; m_pDate = new char[m_nLength+1]; strcpy(m_pDate,rhs.m_pDate);}myCString::myCString(char ch,int nRepeat)//串内有n个ch字符{ m_nLength = nRepeat; m_pDate = new char[m_nLength + 1]; for (int i = 0; i < m_nLength; i++) { m_pDate[i] = ch; } m_pDate[m_nLength] = '\0';}myCString::myCString(const char* cstr, int nLength)//根据字符串常量的一部分进行构造{ m_nLength = strlen(cstr); m_nLength = m_nLength<nLength? m_nLength:nLength;//在cstr字符串的长度和 输入的nLength中选择比较小的 m_pDate = new char[m_nLength+1]; for(int i = 0; i < m_nLength; i++) { m_pDate[i] = cstr[i]; } m_pDate[m_nLength] = '\n';}char myCString::operator[](int nIndex) const{ return m_pDate[nIndex];}myCString& myCString::operator = (const char* cstr){ char* mOldp = m_pDate;//保存旧的指针 effective C++ 条款11 m_nLength = strlen(cstr); m_pDate = new char[m_nLength+1];//分配多一个字节存放\0; strcpy(m_pDate,cstr); delete mOldp; return *this;}myCString& myCString::operator = (const myCString& str){ //不加if里面的也可以,但是加上效率高一点,因为如果str的长度小于原来的长度,那么就不用重新申请空间了。 if (m_nLength > str.m_nLength) { strcpy(m_pDate,str.m_pDate); } else { char* mOldp = m_pDate; m_nLength = str.GetLength(); m_pDate = new char[m_nLength+1]; strcpy(m_pDate,str.m_pDate); delete mOldp; return* this; } }/* //可能出现内存泄漏myCString& myCString::operator +=(const char* p){ m_nLength = m_nLength+strlen(p); char* pNew = new char[m_nLength+1]; strcpy(pNew,m_pDate); strcat(pNew,p); delete []m_pDate; m_pDate = pNew; return *this;}*/myCString& myCString::operator +=(const char* p){ char* pOld = m_pDate; m_nLength = m_nLength+strlen(p); char* pNew = new char[m_nLength+1]; strcpy(pNew,m_pDate); strcat(pNew,p); m_pDate = pNew; delete pOld; return *this;}myCString& myCString::operator +=(const myCString& str){ char* pOld = m_pDate; m_nLength = m_nLength + str.m_nLength;//str.GetLength();//这里刚开始以为str不能直接访问a, //结果也可以,大吃一惊啊,原来只要是成员函数当中的类对象就可以访问私有成员。 char* pNew = new char[m_nLength+1]; strcpy(pNew,m_pDate); strcat(pNew,str.m_pDate); m_pDate = pNew; delete pOld; return *this;}//怀疑pszSrc是其他的myCString对象,不是this的对象不能调用其私有成员??//应该是可以调用的,因为只要调用位置是myCString类的成员函数就够了。myCString myCString::operator+( const char* pszSrc)const //来源不改变,返回一个对象{ myCString rec; rec.m_nLength = m_nLength +strlen(pszSrc); rec.m_pDate = new char[m_nLength+1]; strcpy(rec.m_pDate,m_pDate); strcat(rec.m_pDate,pszSrc); return rec;}//只读函数:让this指针变为了const myString*类型//非只读函数:this指针 原本是myString* 类型//friend CString operator +( const CString& string1, const CString& string2 );//只读函数后面的const与全局函数中左值前面的const达到的效果相同myCString myCString::operator+( const myCString& str) const //来源不改变,返回一个对象{ myCString rec; delete []rec.m_pDate; rec.m_nLength = m_nLength+str.m_nLength; rec.m_pDate = new char[m_nLength+1]; strcpy(rec.m_pDate,m_pDate); strcat(rec.m_pDate,str.m_pDate); return rec;}bool operator!=(const myCString& str1,const myCString& str2) { bool b = strcmp(str1.m_pDate,str2.m_pDate); return b != 0; } bool operator==(const myCString& str1,const myCString& str2) { bool b = strcmp(str1.m_pDate,str2.m_pDate); return !b; } bool operator<(const myCString& str1, const myCString& str2) { return strcmp(str1.m_pDate,str2.m_pDate)<0; } bool operator > (const myCString& str1, const myCString& str2) { return strcmp(str1.m_pDate,str2.m_pDate)>0; } myCString::~myCString(void){ delete []m_pDate;}
3:main.cpp
// String编写.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "myCString.h"#include <iostream>#include <stdio.h>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ /* myCString str1; myCString str2("Hello"); myCString str3("你是谁"); str2 += "World"; myCString str4 = str2; cout<<(const char*)str2<<endl; cout<<str2.GetLength()<<endl; char a[7]="abcdef"; char b[4]="ABC"; strcpy(a,b); for(int i=0;i<7;i++) printf ("%c",a[i]); //说明strcpy不是清除后再赋值 */ myCString str1("Hello"); myCString str2(str1); if (str2 == str1) { cout<<"相等"<<endl; } myCString str3("HH"); if (str3 != str2) { cout<<"不相等"<<endl; } getchar(); return 0;}
自己写的String类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。