29.5. Serializing pointers

Serializing pointer members

CPopDoc has a cGame* _pgame object as its most important member. Serializing the cGame *_pgame pointer takes a bit of care.

Something to realize is that when you load into a CPopDoc, that CPopDoc object will already exist, so it will have been initialized by a constructor call. So the _pgame will in fact be a valid pointer. Whenever you load into a valid pointer variable ptr, you have to call delete on the pointer first, otherwise you’ll have a memory leak caused by the ‘orphaned’ object that the pointer pointed to before you overwrote it with the load. For reasons we’ll now explain, we must use an overloaded ar >> ptr operator to load into a pointer, rather than a call like ptr->Serialize(ar). ...

Get Software Engineering and Computer Games now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.