Say I have an array of chars, which I have allocated on the heap, and which I want to convert into an std::string. Currently I am doing the following:
char *array = new char[size];
WriteIntoArray(array, size);
std::string mystring(array);
delete[] array;
return mystring; // or whatever
From what I read on the internet (http://www.cplusplus.com/reference/string/string/string/), the string constructor performs a copy of the buffer I pass it, leaving me to free the buffer (later, the string frees its internal buffer). What I would like to do is to allocate my buffer, transfer control of it to the string, and then have it free my buffer when it is destructed.
The question initializing std::string from char* without copy looked promising, but my code relies on API calls ("WriteIntoArray" in the above example) which have to write into an array of chars, so I have to create a C-style char* buffer, and cannot convert my code to use only built-in string operations (which was the answer suggested).
Is there some standard way to do this, or should I just write my own string class (ugh)? Thanks!