// 头文件
#pragma once
#include <iostream>
using namespace std;
class CSimString
{
friend ostream &operator<<( ostream &, const CSimString & );
private:
char* pBuf;
int len;
public:
CSimString(void);
CSimString(const char* );
CSimString(CSimString &);
~CSimString(void);
const CSimString& operator =(const CSimString &);
const CSimString& operator +=(const CSimString &);
bool operator ==(const CSimString& ) const;
bool operator !=(const CSimString& ) const;
bool operator <(const CSimString& ) const;
char operator [](int index) const;
CSimString operator()(int index,int subLen) const;
const char* c_str() const;
int getLen(void) const;
};
//cpp文件
#include "stdafx.h"
#include "SimString.h"
#include <string.h>
CSimString::CSimString(void):pBuf(NULL),len(0)
{
pBuf= new char[1];
pBuf[0] = '\0';
}
CSimString::CSimString(const char* str):pBuf(NULL),len(0)
{
if (str)
{
len = strlen(str);
pBuf = new char[len+1];
strcpy(pBuf,str);
}
else
{
pBuf = new char[1];
pBuf[0] = '\0';
}
}
CSimString::CSimString(CSimString& strObj):pBuf(NULL),len(0)
{
if (strObj.pBuf)
{
len=strlen(strObj.pBuf);
pBuf = new char[len+1];
strcpy(pBuf,strObj.pBuf);
}
else
{
pBuf = new char[1];
pBuf[0] = '\0';
}
}
CSimString::~CSimString(void)
{
if (pBuf)
{
delete pBuf;
pBuf = NULL;
}
}
const CSimString& CSimString::operator =(const CSimString& strObj)
{
if (this != &strObj)
{
delete pBuf;
pBuf = NULL;
if (strObj.pBuf)
{
len = strlen(strObj.pBuf);
pBuf = new char[len+1];
strcpy(pBuf,strObj.pBuf);
}
else
{
pBuf = new char[1];
pBuf[0]='\0';
}
}
else
return *this;
}
const CSimString& CSimString::operator +=(const CSimString& strObj)
{
int newLen = len + strObj.len;
char *tmpPtr = new char[newLen +1];
strcpy(tmpPtr,pBuf);
strcpy(tmpPtr+len,strObj.pBuf);
delete pBuf;
pBuf = tmpPtr;
len = newLen;
return *this;
}
bool CSimString::operator ==(const CSimString& strObj) const
{
return strcmp(pBuf,strObj.pBuf) ==0;
}
bool CSimString::operator != (const CSimString& strObj) const
{
return !(*this == strObj);
}
bool CSimString::operator <(const CSimString& strObj) const
{
return strcmp(this->pBuf,strObj.pBuf)<0;
}
char CSimString::operator [](int index) const
{
if (index <0 || index>len)
{
return '\0';
}
return pBuf[index];
}
CSimString CSimString::operator()(int index,int subLen) const
{
if (index <0 || index >len || subLen >len)
{
return "";
}
int tmpLen;
if (subLen ==0 ||index+subLen >len)
{
tmpLen = len-index;
}
else
tmpLen = subLen;
char *tmpPtr = new char[tmpLen +1];
strncpy(tmpPtr,&pBuf[index],tmpLen);
tmpPtr[tmpLen]='\0';
CSimString newStr(tmpPtr);
delete tmpPtr;
return newStr;
}
const char* CSimString::c_str() const
{
return this->pBuf;
}
ostream& operator <<(ostream& output, const CSimString &ss)
{
return output<<ss.pBuf;
}
int CSimString::getLen(void) const
{
return len;
}
测试:
#include "stdafx.h"
#include "SimString.h"
int _tmain(int argc, _TCHAR* argv[])
{
CSimString tmp(NULL);
CSimString tmp2("fsfdsfds");
CSimString tmp3(tmp2);
CSimString tmp4(tmp3);
CSimString str1("123456789");
CSimString str2("34");
CSimString str3("12");
bool m = (str1 == str3);
CSimString str4 = str1(3,9);
std::cout<<str4<<endl;
return 0;
}