Unfortunately you will have to write the whole serialization code for yourself in C++. If all you have to do is saving out only an array of objects and then loading it you will get away with quite a simple solution. If you need a much more general serialization support then you will have to use a library that aims your problem or write your own framework but even using such a solution is a bit messy.
Lets assume all you want to save is an array of object pointers to the base class.
Saving:
first you serialize the size of the array, and then the objects one-by-one. You can do the object serialization by introducing a virtual Serialize(Serializer& ser) method to your base class and you call this serialize method on each object while you are iterating over the array. Each object must save a unique identifier that identifies the given type/class and then the data that is needed to restore the inner state. The identifier must be a unique id among the serialzable types/classes, this can be for example the name of the class for the sake of simplicity.
Loading:
First you load the size of the array and set the size of your actual array instance. Then you iterate over the array and for each item in the array you call a magic function that reads creates an object by reading it from the stream. This magic function must somehow know every identifier that can occur in your Serialize() function calls when saving. The magic function reads the type identifier, checks which class must be instantiated, instatiates the class and calls its virtual Deserialize(Deserializer& dser) method that reads the rest of the data from the stream. This magic function check the identifier with an if-else construct or gets the creator function from a registry in which you can register new types dynamically.
EDIT: Iam in good mood so I created you a snippet that demonstrates what I described above:
class CSerializer
{
public:
void Write(const char&);
void Write(const int&);
void Write(const size_t&);
void Write(const bool&);
void Write(const float&);
void Write(const char*);
void Write(const std::string&);
};
class CDeserializer
{
public:
void Read(char&);
void Read(int&);
void Read(size_t&);
void Read(bool&);
void Read(float&);
void Read(std::string&);
};
class CBaseClass
{
public:
virtual ~CBaseClass() {}
virtual void Serialize(CSerializer& ser) = 0;
virtual void Deserialize(CDeserializer& dser) = 0;
};
class CDerivedClass : public CBaseClass
{
public:
virtual void Serialize(CSerializer& ser)
{
ser.Write("CDerivedClass");
ser.Write(m_DerviedClassState);
}
virtual void Deserialize(CDeserializer& dser)
{
dser.Read(m_DerviedClassState);
}
private:
int m_DerviedClassState;
};
class CDerivedClass2 : public CBaseClass
{
public:
virtual void Serialize(CSerializer& ser)
{
ser.Write("CDerivedClass2");
ser.Write(m_DerviedClassState);
ser.Write(m_DerviedClassState2);
}
virtual void Deserialize(CDeserializer& dser)
{
dser.Read(m_DerviedClassState);
dser.Read(m_DerviedClassState2);
}
private:
std::string m_DerviedClassState;
bool m_DerviedClassState2;
};
std::vector<CBaseClass*> my_array;
void Serialize(CSerializer& ser)
{
size_t size = my_array.size();
ser.Write(size);
for (size_t i=0; i<size; ++i)
my_array[i]->Serialize(ser);
}
CBaseClass* DeserializeObject(CDeserializer& dser)
{
std::string class_id;
dser.Read(class_id);
CBaseClass* obj = NULL;
if (class_id == "CDerivedClass")
obj = new CDerivedClass;
else if (class_id == "CDerivedClass2")
obj = new CDerivedClass2;
obj->Deserialize(dser);
return obj;
}
void Deserialize(CDeserializer& dser)
{
size_t size;
dser.Read(size);
my_array.resize(size);
for (size_t i=0; i<size; ++i)
my_array[i] = DeserializeObject(dser);
}