Original Post
hi there, I'm having trouble trying to work out how to serialize an object that contains a pointer to another object. I have an array of objects all of which point to another object, I need to serialize the object once and then restore these pointers, the trouble is trying to create a good interface for doing this. currently I have a class that takes all c++ primitive data types and saves them to file, it also takes an undefined class (using templates) and calls this class's serialize method: this is similar to boost. I was thinking I could store the addresses of every object in the Archive class when they pass through the member functions as and array of and then when a (<<)member function is called just look up the address in the array and then save the index instead: this means that the object is serialized once only but it seems very inefficient having to lookup every object you have serialized before just to serialize an object. Is there a better way to do this?
class Archive
{
public:
void operator <<(int i);
void operator <<(float f);
void operator <<(bool b);
template <class T>
void operator <<(const T& obj)
{
obj.serialize(this);
}
};
class Archive
{
public:
void operator <<(int i);
void operator <<(float f);
void operator <<(bool b);
template <class T>
void operator <<(const T* obj)
{
for(int i = 0; i < adresses.size(); ++i)
{
// already been serialized
if(obj == adresses)
{
// write i to file
return;
}
}
adresses.push_back(obj);
obj->serialize(this);
}
private:
std::vector<const void*> addresses;
};