Write pointer to string,delete pointer,and load pointer from string?
It's possible, but it won't do what you think it will do. Saving entire object as a string is called serialization - see Boost.Serialization (or Protocol Buffers, as suggested in comments) if you need that.
It's possible to do those operations, but they won't have the effect you're (probably) after.
Writing the pointer to string will only store the pointer value, i.e. the address of the pointed-to object. This is a string of more or less constant length, like 0x7f2b93c91780 (on a 64-bit system). Naturally, this doesn't capture any of the state of the actual object.
When you use delete
on the pointer, the memory pointed at will be returned to the system; it's no longer yours to use. The pointer itself is not deleted, the operation only affects the memory being pointed at. Also, the pointer's value doesn't actually change when you use delete
on it. Thus, there's no difference if you now re-load it by reading it in from a string stored somewhere else: it's still pointing at memory you no longer own, and thus can't read or write without invoking undefined behavior.
Like PiotrLegnica suggested, you need to serialize the entire object into a string, and then re-create the object from the serialized version (de-serialize it).