我試圖建立一個實用工具類,我可以重新使用,即轉換一個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