习作! (转)

习作! (转)[@more@]

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
#include
#include

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;
}


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-996526/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-996526/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值