我试图建立一个实用工具类,我可以重新使用,即转换一个std::string to a char*
:功能的返回值的char *返回垃圾
char* Foo::stringConvert(std::string str){
std::string newstr = str;
// Convert std::string to char*
boost::scoped_array<char> writable(new char[newstr.size() + 1]);
std::copy(newstr.begin(), newstr.end(), writable.get());
writable[newstr.size()] = '\0';
// Get the char* from the modified std::string
return writable.get();
}
当我试图加载从输出的代码工作在stringConvert函数内,但是当在我的应用程序的其他部分中使用时,此函数返回垃圾。
例如:
Foo foo;
char* bar = foo.stringConvert(str);
上面的代码返回垃圾。 有没有解决这类问题的方法?
什么是可写的? – cppcoder
如果你没有遗漏代码,回答这个问题会更容易。 –
@cppcoder boost :: scoped_array可写(new char [newstr.size()+ 1]); –
xybrek