Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » String to std::string conversion
Re: String to std::string conversion [message #32852 is a reply to message #32850] Wed, 15 June 2011 07:30 Go to previous messageGo to previous message
dolik.rce is currently offline  dolik.rce
Messages: 1791
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hi GaroRobe

Lances explanation about the const char* and std::string is correct. However, his solution is not, the const_cast can only change the constness of variable, not its type. Also, casting Upp::String to std::string is not a good idea anyway, as their internal representation probably differs.

Anyway, there is quite a number of possible solutions:
std::string tempPath;
FileSel file;
{...}
tempPath = ~file[i]; // converts the Upp::String to const char*
//OR
tempPath = file[i].Begin(); // basically the same as above
//OR
tempPath = std::string(~file[i],file[i].GetLength()); // create a new std::string with the same content and length
//OR
tempPath = std::string(~file[i],file[i].GetLength()); // again, more verbose variation on the previous line

Not that the first two solutions contain potential bug. If there are zero bytes ('\0') in the string, only part up until the first null would get copied. The last two solutions always copy the entire string properly, so I would recommend you to use one of those Wink


Best regards,
Honza
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: MSVC 10 to Upp conversion
Next Topic: HttpClient and language choice (HTTP_ACCEPT_LANGUAGE)
Goto Forum:
  


Current Time: Mon May 12 13:16:40 CEST 2025

Total time taken to generate the page: 0.02660 seconds