使用C++编写类似MFC的CString,部分功能,重新复习使用操作符重载

类别:编程语言 点击:0 评论:0 推荐:

.h 文件  类的定义

class CString
{
public:
 CString();
 CString(const CString &strTag);
 CString(const char* szTag);

 virtual ~CString();
 
public:
 char* GetBuffer();

public:
 CString& operator=(const char* strTag);
 CString& operator=(const CString& strTag);
 CString& operator+(const char* szAdd);
 CString& operator+(const CString& strTag);

public:
 void InitData(const char* szInit);
 friend ostream& operator<< ( ostream& os, const CString& strOut );

private:
 char* m_pszData;
 int m_nBufLen;
};

.CPP 文件

#include "CString.h"


CString::CString()
{
 m_pszData=NULL;
 m_nBufLen = 1;
 m_pszData = new char[m_nBufLen];
 m_pszData[0] = '\0';
}

CString::~CString( )
{
 if( m_pszData )
 {
  delete m_pszData;
  m_pszData = NULL;
  m_nBufLen = 0;
 }
}

void CString::InitData(const char *szInit)
{
 if( m_pszData )
 {
  delete m_pszData;
  m_pszData = NULL;
  m_nBufLen = 0;
 }
 
 int nLen = strlen(szInit);
 m_pszData = new char[nLen+1];
 if( m_pszData )
 {
  strncpy(m_pszData,szInit,nLen);
  m_pszData[nLen] = '\0';
  m_nBufLen = nLen+1;
 }
}

CString::CString(const CString &strTag)
{
 m_pszData=NULL;
 m_nBufLen = 0;
 InitData(strTag.m_pszData);
}

CString::CString(const char* szTag)
{
 m_pszData=NULL;
 m_nBufLen = 0;
 InitData(szTag);
}

CString& CString::operator +(const char* szAdd)
{
 int nLenthis = strlen(m_pszData);
 int nLenAdd = strlen(szAdd);
 char* pAdd = new char[nLenthis+nLenAdd+1];
 strncpy(pAdd,m_pszData,nLenthis+1);
 strcat(pAdd,szAdd);

 static CString strAdd;
 strAdd = pAdd;
 delete pAdd;

 return strAdd;
}

CString& CString::operator+(const CString& strTag)
{
 return operator+(strTag.m_pszData);
}


CString& CString::operator =(const char *szTag)

 int nTagLen = strlen(szTag);
 char* pszAdd = new char[nTagLen+1];
 if( pszAdd )
 {
  strncpy(pszAdd,szTag,nTagLen+1);
  m_nBufLen = nTagLen+1;

  delete m_pszData;
  m_pszData = pszAdd;
 }
 return *this;
}

CString& CString::operator =(const CString &strTag)
{
 return operator=(strTag.m_pszData);
}

ostream& operator<< ( ostream& os, const CString& strOut )
{
 os<<strOut.m_pszData;
 return os;
}

char* CString::GetBuffer()
{
 return m_pszData;
}

///////////////////////////////////////////////////////////////////////
//
//   测试程序 The test program
//

void main()
{
 CString str1("Hello");
 CString str2("world!");
 CString str3;
 
 cout<<"HTTP://WWW.5701.COM   EM:[email protected]"<<endl;
 cout<<"Class CString by Wenjin 2005.3"<<endl;
 cout<<"----------------------------------------"<<endl;
 cout<<"define CString str1(\"Hello\");"<<endl;
 cout<<"define CString str2(\"world!\");"<<endl;
 cout<<"define CString str3;"<<endl<<endl;
 cout<<"Para str1 = "<<str1<<endl;
 cout<<"Para str2 = "<<str2<<endl;
 cout<<"Para str3 = "<<str3<<endl<<endl;

 cout<<"str1 + \' \' + str2 = "<<str1+" "+str2<<endl<<endl;
 cout<<"Do str3=str1+\' \'+str2"<<endl;
 str3 = str1+" "+str2;
 cout<<"Now str3 = "<<str3<<endl<<endl;

 cout<<"Now define CString str4(str3);"<<endl;
 CString str4(str3);
 cout<<"Para str4 = "<<str4<<endl<<endl;
 cout<<"Output ended.Press enter to quit"<<endl;
 cin.peek();
}

本文地址:http://com.8s8s.com/it/it22726.htm