![]() |
#2
BlueMouse2008-09-23 10:59
找出原因了!晕!
|

#include <windows.h>
class CA
{
private:
const char *tValue;
public:
CA& operator=(const char* tNewValue);
CA& operator+=(const char* tNewValue);
operator const char*()
{
return this->tValue;
}
};
CA& CA::operator=(const char* tNewValue)
{
this->tValue = tNewValue;
return *this;
}
CA& CA::operator+=(const char* tNewValue)
{
char *pSz;
pSz = new char(strlen(this->tValue)+strlen(tNewValue)+1);
strcat(pSz,this->tValue);
strcat(pSz,tNewValue);
delete [] this->tValue;
this->tValue = pSz;
return *this;
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCommandLine,
int nShowCmd)
{
CA myca;
myca = "TEST INFORMATION\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
MessageBoxA(NULL,
myca,
"Debug information",
MB_OK|MB_ICONINFORMATION);
return 0;
}
class CA
{
private:
const char *tValue;
public:
CA& operator=(const char* tNewValue);
CA& operator+=(const char* tNewValue);
operator const char*()
{
return this->tValue;
}
};
CA& CA::operator=(const char* tNewValue)
{
this->tValue = tNewValue;
return *this;
}
CA& CA::operator+=(const char* tNewValue)
{
char *pSz;
pSz = new char(strlen(this->tValue)+strlen(tNewValue)+1);
strcat(pSz,this->tValue);
strcat(pSz,tNewValue);
delete [] this->tValue;
this->tValue = pSz;
return *this;
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCommandLine,
int nShowCmd)
{
CA myca;
myca = "TEST INFORMATION\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
myca += "0123456789\n";
MessageBoxA(NULL,
myca,
"Debug information",
MB_OK|MB_ICONINFORMATION);
return 0;
}
可以运行! 但是字符串前面不知道怎么会多出一些乱码来!,难道是连接字符串的时候tValue的指向被修改了? 问题是肯定出在CA::operator+=里的! 但是是什么问题?如何解决呢?请懂的人教教!