c++ - How Do I Use a Variable in new[]'s Value Initialization -
so when new
ing array of char
s can value initialize:
const char* foo = new char[4]{'j', 'o', 'n', '\0'};
what want know how use variable in initializer_list
:
const string initializer{"jon"}; const char* foo = new char[4]{initializer.c_str()}; // doesn't work, can make work?
you can use variable, can't use string , expect compiler magically split up.
const char* ip = initializer.c_str(); const char* foo = new char[4]{ip[0], ip[1], ip[2], ip[3]};
once have variable length, though, doesn't work @ all. have allocate , use string::copy
fill array in separate step.
Comments
Post a Comment