I am using the std::string type for my string manipulations.
However, sometimes I need to keep the raw char* pointer, even after the original std::string object is destroyed (yes, I know the char* pointer references the HEAP and must eventually be disposed of).
However, looks like there is no way to detach the raw pointer from the string or is it?
Maybe I should use another string implementation?
Thanks.
EDIT
Folks, please do not confuse detaching with copying. The essence of detaching is for the string object to relinquish its ownership on the underlying buffer. So, had the string had the detach
method, its semantics would be something like this:
char *ptr = NULL;
{
std::string s = "Hello world!";
ptr = s.detach(); // May actually allocate memory, if the string is small enough to have been held inside the static buffer found in std::string.
assert(s == NULL);
}
// at this point s is destroyed
// ptr continues to point to a valid HEAP memory with the "Hello world!" string in it.
...
delete ptr; // need to cleanup