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   |
|
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 
Best regards,
Honza
|
|
|
Goto Forum:
Current Time: Mon May 12 16:00:21 CEST 2025
Total time taken to generate the page: 0.00790 seconds
|