习作!

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

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;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航