我试图从一个函数返回一个本地对象
我有这个问题
如果我返回本地创建的对象,它返回 null
DString DString :: operator + (const char* param)
{
// Variable definition
int nSize = ( this->GetLength() + (strlen(param)));
// Create a new char array in the opropriate size
char* cstrNewString = new char[nSize + 1];
// Append data
strcpy(cstrNewString, this->_cstrString);
strcat(cstrNewString, (char*)param);
cstrNewString[nSize] = '\0';
// Create a new DString to return
DString dstNewData(cstrNewString);
// Clean up
delete[] cstrNewString;
// Return data
return (dstNewData); // Null!
如果我做同样的事情只在返回行中创建一个新对象,例如:
DString DString :: operator + (const char* param)
{
// Variable definition
int nSize = ( this->GetLength() + (strlen(param)));
// Create a new char array in the opropriate size
char* cstrNewString = new char[nSize + 1];
// Append data
strcpy(cstrNewString, this->_cstrString);
strcat(cstrNewString, (char*)param);
cstrNewString[nSize] = '\0';
// Create a new DString to return
DString dstNewData(cstrNewString);
// Clean up
delete[] cstrNewString;
// Return data
return (DString(dstNewData.ToCharArray())); // Not null, returns correctly!
它正确返回..为什么要这样做,我该如何解决?