On 10/16/2015 9:35 PM, Christopher Pisz wrote:
> On 10/16/2015 12:47 PM, Alf P. Steinbach wrote:
>> vector< wchar_t const* > types( things.size() );
>> for( auto& p : types ) { p = things.c_str(); }
>
>
> p = things.c_str();
>
> Assigning std::string::c_str() to type char * does not work.
ITYM, "assigning std::wstring::c_str() to type char* does not work". The
solution to that involves using the Unicode API functions, not the
1990's Windows ANSI functions. Define the macro symbol UNICODE before
including <windows.h>. Use wchar_t, not char. Remember to add const as
appropriate: assigning std::wstring::c_str() result to wchar_t* is not
valid, but wchar_t const* is OK.
> When the
> string goes out of scope, so does its buffer, I believe.
Yes. Don't let the vector with the strings go out of scope yet.
Quting myself on that, "You do need to be careful with zero size array
and lifetimes and modifying operations and such.".
I.e., there are also a few more such possible pitfalls.
> The vector has to have each element point to allocated memory, or where
> are we going to store the string?
Uh, I think this refers to some flawed understanding.
Maybe that has to do with failing to quote the relevant declarations, above.
> Nor does variable 'things' have a c_str() method, it's a vector. So, the
> for(auto...syntax can't be used, because you need an index, no?
It can be used, with some backing (e.g. à la Python's enumerate), but
since you don't have that already in your toolbox it's simpler to just
use an index-based loop. Sorry for just sketching the code.