我有这样的代码块strstream
。我把它转换为sstream
如下。我不确定,但我认为printStream->str()
返回一个字符串对象,其中包含由指向的流缓冲区中的内容的副本(临时)printStream
,然后你调用c_str()
并得到一个const char *
,然后抛弃常量性,然后返回函数作用域之外的指针。我认为由于它是一个临时值,您将从中返回printStream->str()
,您将使用指向该函数外部已释放内存的指针。我该怎么做?
char * FieldData::to_string() const
{
if(printStream)
return printStream->str();
FieldData* notConst = (FieldData*) this;
notConst->printStream = new std::ostrstream;
// check heap sr60315556
if (notConst->printStream == NULL)
return NULL;
*(notConst->printStream) << "Invalid Field Type";
*(notConst->printStream) << '\0';
return printStream->str();
}
char * FieldData::to_string() const
{
if(printStream)
return const_cast<char *>(printStream->str().c_str());
FieldData* notConst = (FieldData*) this;
notConst->printStream = new std::ostringstream;
// check heap sr60315556
if (notConst->printStream == NULL)
return NULL;
*(notConst->printStream) << "Invalid Field Type";
*(notConst->printStream) << '\0';
return const_cast<char *>(printStream->str().c_str());
}
将返回类型更改为std::string
并返回一个std::string
直接反对。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)