. H file class definition
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 File
# 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;
}
//////////////////////////////////////// ///////////////////////////////
//
// Test program the test program
//
Void main ()
{
Cstring str1 ("hello ");
Cstring str2 ("world! ");
Cstring str3;
Cout <"http: // www.5701.com EM: Wenjin@5701.com" <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;
Cout <"Para str1 =" <str1 <Endl;
Cout <"Para str2 =" <str2 <Endl;
Cout <"Para str3 =" <str3 <Endl;
Cout <"str1 +/'+ str2 =" <str1 + "" + str2 <Endl;
Cout <"Do str3 = str1 +/'+ str2" <Endl;
Str3 = str1 + "" + str2;
Cout <"Now str3 =" <str3 <Endl;
Cout <"now define cstring str4 (str3);" <Endl;
Cstring str4 (str3 );
Cout <"Para str4 =" <str4 <Endl;
Cout <"output ended. Press enter to quit" <Endl;
Cin. Peek ();
}