C++这个动东东以前看了始终没有写过代码,昨天看了今天花了一天时间,调了一个小类,让大家指点指点:
/*********************** tstring.h **********************************
#ifndef TSTRING_H
#define TSTRING_H
#define NULL 0
#define TRUE 1
#define FALSE 0
typedef long BOOL;
typedef unsigned long DWORD;
class TSTRING
{
public:
TSTRING(const char * pStr = NULL);
~TSTRING();
void SetNewString(const char * pStr = NULL);
char * GetString(void);
DWORD GetStringLen(void);
void Print(void);
/**********************************************************
**There are some side effects in assignment(=) overloading
** with plus(+) overloading case(TSTRING &, TSTRING &).
***********************************************************/
file://TSTRING & operator = (const char * pStr);
friend TSTRING & operator + (TSTRING & oStr, const char * pStr);
friend TSTRING & operator + (const char * pStr, TSTRING & oStr);
friend TSTRING & operator + (TSTRING & oStr1, TSTRING & oStr2);
protected:
BOOL bNewAlloc; // Indicate whether memory is allocated
char * pHeader; // Indicate the first character of the string
DWORD dLen; // Indicate the number of characters
private:
char cNull; // Represent the string when it is null.
};
#endif
/******************* tstring.cpp **************************************
#include <malloc.h>
#include <string.h>
#include <iostream>
using namespace std;
#include "tstring.h"
TSTRING :: TSTRING(const char * pStr)
{
if (pStr == NULL) {
dLen = 0;
// Set pHeaer to point a null string
cNull = 0;
pHeader = &cNull;
bNewAlloc = FALSE;
}
else {
dLen = strlen(pStr);
pHeader = (char *) malloc(dLen + 1);
strcpy(pHeader, pStr);
bNewAlloc = TRUE;
}
}
TSTRING :: ~TSTRING()
{
// If the memory used by the string is allocated originally
// by compiler, MUST NOT free it, or cause a exception!
if (bNewAlloc)
free(pHeader);
}
void TSTRING :: SetNewString(const char * pStr)
{
long lLen = strlen(pStr);
// If the space is exactly enough, NO NEED to allocate memory.
if (dLen != lLen) {
if (bNewAlloc)
free(pHeader);
pHeader = (char *) malloc(lLen + 1);
dLen = lLen;
bNewAlloc = TRUE;
}
strcpy(pHeader, pStr);
}
char * TSTRING :: GetString(void)
{
return pHeader;
}
DWORD TSTRING :: GetStringLen(void)
{
return dLen;
}
void TSTRING :: Print(void)
{
cout << pHeader;
}
/*
TSTRING & TSTRING :: operator = (const char * pStr)
{
if (bNewAlloc)
free(pHeader);
dLen = strlen(pStr);
pHeader = (char *) malloc(dLen + 1);
strcpy(pHeader, pStr);
return * this;
}
*/
TSTRING & operator + (TSTRING & oStr, const char * pStr)
{
TSTRING *pNewStr = new TSTRING;
long lLen;
lLen = oStr.dLen + strlen(pStr);
pNewStr->pHeader = (char *) malloc(lLen + 1);
strcpy(pNewStr->pHeader, oStr.pHeader);
strcpy(pNewStr->pHeader + oStr.dLen, pStr);
pNewStr->bNewAlloc = TRUE;
pNewStr->dLen = lLen;
return * pNewStr;
}
TSTRING & operator + (const char * pStr, TSTRING & oStr)
{
TSTRING *pNewStr = new TSTRING;
long lLen;
lLen = strlen(pStr);
pNewStr->pHeader = (char *) malloc(lLen + oStr.dLen + 1);
strcpy(pNewStr->pHeader, pStr);
strcpy(pNewStr->pHeader + lLen, oStr.pHeader);
pNewStr->bNewAlloc = TRUE;
pNewStr->dLen = lLen + oStr.dLen;
return * pNewStr;
}
TSTRING & operator + (TSTRING & oStr1, TSTRING & oStr2)
{
TSTRING *pNewStr = new TSTRING;
pNewStr->dLen = oStr1.dLen + oStr2.dLen;
pNewStr->pHeader = (char *) malloc(pNewStr->dLen + 1);
strcpy(pNewStr->pHeader, oStr1.pHeader);
strcpy(pNewStr->pHeader + oStr1.dLen, oStr2.pHeader);
pNewStr->bNewAlloc = TRUE;
return * pNewStr;
}
/************************* learn.cpp (including main entry function) ************
#include "tstring.h"
int main(int argc, char * argv[])
{
TSTRING str1("Original string!\n");
str1.Print();
str1.SetNewString("New string!");
str1.Print();
TSTRING & str2 = str1;
str2 = "pre_" + str1 + "_post!";
str2.Print();
return 0;
}
本文地址:http://com.8s8s.com/it/it2804.htm