String.h
//字符串封装类名CString
class CString
{
friend CString operator + (const char *pStr, const CString &r); //"string" + obj 声明为友元,提供一方的private数据
friend istream& operator >> (istream &in, CString &r); //cin >>运算符重载
friend ostream& operator << (ostream &out, CString &r); //cout <<运算符重载
public:
CString(); //无参构造
CString(const char *pStr); //类型转换构造,带一个参数的构造
CString(const CString &r);
~CString(); //析构函数
char *StrCpy(const char *pStrSrc);
char *StrCpy(/*CString *const this,*/const CString &r);
bool StrCmp(/* const CString *const this,*/const CString &r) const;
bool StrCmp(const char *pStr) const;
// operator int();
// operator char *();
CString& operator = (const CString &r);
CString& operator = (const char *pStr);
bool operator == (const CString &r) const;
bool operator == (const char *pStr) const;
CString operator +(const CString &r) const; //obj + obj
CString operator +(const char *pStr) const; // obj + "String"
CString operator += (const CString &r); // obj += obj
CString operator += (const char *pStr); // obj += "String"
int GetLength() const;
const char *GetStr() const;
char operator[](int nIndex);
bool Empty();
private: //受保护的成员函数,不需要校验,在外部已经校验
int StrLen(const char *pStr) const;
char *Copy(const char *pStrSrc);
char *Add(const char *pStr, int nBufSize) const;
void Release();
bool Cmp(const char *pStr) const;
private:
char *m_pStr;
unsigned int m_nSize;
};
//string == obj
bool operator == (const char *pStr, const CString &r);
String.cpp
#include "stdafx.h"
#include "String.h"
//
// Construction/Destruction
//
CString::CString() //无参构造
{
m_pStr = NULL;
m_nSize = 0;
}
CString::CString(const char *pStr) //类型转换构造, 一个参数的构造
{
m_pStr = NULL;
m_nSize = 0;
if(NULL != pStr)
{
StrCpy(pStr);
}
}
CString::CString(const CString &r) //深复制,分配两个内存
{
m_pStr = NULL;
m_nSize = 0;
if(this == &r)
{
return ;
}
if(0 != r.m_nSize)
{
StrCpy(r.m_pStr);
}
}
CString::~CString() //析构函数,释放
{
delete []m_pStr;
}
/*CString(const CString &r) //浅复制,共享一个内存。浅复制和深复制不能同时存在一个类中,只能选择其一
{
m_pStr = NULL;
m_nSize = 0;
}*/
//字符串复制string
char* CString::StrCpy(const char *pStrSrc)
{
if(NULL != pStrSrc)
{
Release();
m_nSize = StrLen(pStrSrc);
m_pStr = new char[m_nSize + 1];
Copy(pStrSrc);
}
return m_pStr;
}
//字符串复制obj
char* CString::StrCpy(const CString &r)
{
Release();
if(0 != r.m_nSize)
{
m_nSize = r.m_nSize;
m_pStr = new char[m_nSize + 1];
Copy(r.m_pStr);
}
return m_pStr;
}
/*//成员类型转换函数
CString::operator int()
{
return m_nSize;
}
//成员类型转换函数
CString::operator char *()
{
return m_pStr;
}*/
//赋值运算符重载
CString& CString::operator = (const CString &r) //参数为CString对象
{
if(this != &r) //若不是将自己给自己
{
if(0 != r.m_nSize)
{
StrCpy(r); //使用字符串函数重载函数
//StrCpy(r.m_pStr);
}
}
return *this;
}
//赋值运算符重载
CString& CString::operator = (const char *pStr) //参数为字符串,如果不写这个,系统自动调用默认字符串过程例子:
{ //obj = "test" ->(1)临时对象 = CString("test"); (2)obj = 临时对象
if(NULL != pStr) //如果写了这个函数,就省去了上面的临时对象过程,称作以代码空间换取效率
{
StrCpy(pStr);
}
return *this;
}
// obj == obj
bool CString::operator == (const CString &r) const
{
return StrCmp(r);
}
//obj == string
bool CString::operator == (const char *pStr) const
{
return ((NULL != pStr)?StrCmp(pStr):false);
}
//string == obj
bool operator == (const char *pStr, const CString &r)
{
if(NULL != pStr)
{
return r==pStr;
}
return false;
}
//获得字符串大小
int CString::GetLength() const
{
return m_nSize;
}
bool CString::Cmp(const char *pStr) const
{
for(int i = 0; i < m_nSize; i++)
{
if(m_pStr[i] != pStr[i])
{
return false;
}
}
return true;
}
//获得字符串
const char* CString::GetStr() const
{
return m_pStr;
}
//下标运算符
char CString::operator[](int nIndex)
{
return m_pStr[nIndex];
}
bool CString::Empty()
{
if(NULL != m_pStr)
{
return false;
}
return true;
}
//受保护的成员函数,不需要校验,在外部已经校验
int CString::StrLen(const char *pStr) const //获得字符串的长度
{
int nSize = 0;
for(; '\0' != *pStr++; nSize++);
return nSize;
}
char* CString::Copy(const char *pStrSrc) //字符串复制
{
char *pStr = m_pStr;
for(; '\0' != (*pStr++ = *pStrSrc++); );
return m_pStr;
}
void CString::Release() //初始化函数
{
if(NULL != m_pStr)
{
delete []m_pStr;
m_pStr = NULL;
m_nSize = 0;
}
}
// string
bool CString::StrCmp(const char *pStr) const
{
if(NULL == pStr && m_nSize != StrLen(pStr))
{
return false;
}
return Cmp(pStr);
}
//obj
bool CString::StrCmp(const CString &r) const
{
if(this != &r)
{
if(m_nSize != r.m_nSize || NULL == r.m_pStr || NULL == m_pStr)
{
return false;
}
return Cmp(r.m_pStr);
}
return true;
}
char *CString::Add(const char *pStr, int nBufSize) const
{
char *pBuf = new char[nBufSize];
char *pTemp = pBuf;
for(int i = 0; i < m_nSize; i++)
{
pTemp[i] = m_pStr[i];
}
if(NULL != pStr)
{
int j = 0;
for(i = m_nSize; j < nBufSize - m_nSize; j++, i++)
{
pTemp[i] = pStr[j];
}
}
return pBuf;
}
CString CString::operator +(const CString &r) const //obj + obj
{
CString obj;
if(NULL != m_pStr && NULL != r.m_pStr)
{
char *pBuf = Add(r.m_pStr, m_nSize+r.m_nSize+1);
obj = pBuf;
delete []pBuf;
}
return obj;
}
CString CString::operator +(const char *pStr) const // obj + "String"
{
CString obj;
if(NULL != m_pStr && NULL != pStr)
{
int nSize = (NULL != pStr)?StrLen(pStr):0;
char *pBuf = Add(pStr, m_nSize+nSize+1);
obj = pBuf;
delete []pBuf;
}
return obj;
}
CString operator + (const char *pStr, const CString &r) //"String" + obj
{
if(NULL != pStr && NULL != r.m_pStr)
{
CString temp = pStr;
return temp+r;
}
return NULL;
}
CString CString::operator += (const CString &r) // obj += obj
{
*this = *this + r;
return *this;
}
CString CString::operator += (const char *pStr) // obj += "String"
{
*this = *this + pStr;
return *this;
}
istream& operator >> (istream &in, CString &r) //cin
{
char szBuf[4096] = "";
in >> szBuf;
r = szBuf;
return in;
}
ostream& operator << (ostream &out, CString &r) //cout
{
if(!r.Empty())
{
out<<r.m_pStr; //Cur:friend || r.GetStr()
}
return out;
}
#include "stdafx.h"
#include "String.h"
int main(int argc, char* argv[])
{
CString obj = "This is a ";
CString obj1 = obj + "test";
cout<<obj<<endl;
cout<<obj + "Test"<<endl;
cout<<obj1<<endl;
//CString obj3 = obj + obj1;
// cout<<obj1.GetStr()<<endl;
// obj = "Test" + obj;
// cout<<obj.GetStr()<<endl;
return 0;
}