#include <stdio.h>
#include <STDLIB.H>
#include <string.h>
classCString
{
public:
CString();
CString(constchar* pszBuffer);
~CString();
CString(constCString& other);
constCString& Operator=(constCString& other);
PRivate:
char* m_pszBuffer;;
};
CString::CString()
{
printf("CString::CString/n");
m_pszBuffer= NULL;
return;
}
CString::CString(constchar* pszBuffer)
{
printf("CString::CString(const char* pszBuffer)/n");
m_pszBuffer= pszBuffer!= NULL? strdup(pszBuffer) : NULL;
return;
}
CString::~CString()
{
printf("%s/n", __func__);
deletem_pszBuffer;
m_pszBuffer= NULL;
return;
}
CString::CString(constCString& other)
{
if(this== &other)
{
return;
}
printf("CString::CString(const CString& other)/n");
m_pszBuffer= other.m_pszBuffer!= NULL? strdup(other.m_pszBuffer) : NULL;
}
constCString& CString::operator=(constCString& other)
{
printf("const CString& CString::operator=(const CString& other)/n");
if(this== &other)
{
return*this;
}
if(m_pszBuffer!= NULL)
{
free(m_pszBuffer);
m_pszBuffer= NULL;
}
m_pszBuffer= other.m_pszBuffer!= NULL? strdup(other.m_pszBuffer) : NULL;
return*this;
}
voidtest(CStringstr)
{
CStringstr1= str;
return;
}
intmain(intargc, char* argv[])
{
CStringstr;
CStringstr1= "test";
CStringstr2= str1;
str1= str;
CStringstr3= str3;
test(str);
return0;
}原文地址:http://dev.csdn.net/author/absurd/082775af05e44a4db1e9cdb4977687b2.Html
新闻热点
疑难解答