I am writing a serialize and deserialize method, and I encountered a problem in the implementation of deserialize: I can’t new a value_type, which is actually a Skill*.
template <class T >
static istream &DeSerializePVector(istream& istream_, T& container)
{
typedef typename T::value_type ElementType;
size_t size;
istream_ >> size;
container.reserve(size);
container.resize(size);
for(typename T::iterator ite = container.begin(); ite != container.end(); ite++)
{
*ite = new *ElementType; //how can I initialize this type?
(*ite)->DeSerialize(istream_);
}
return istream_;
}
int main()
{
Skill* disease = Factory::CreateSkill ( SKILLTYPE_DISEASE );
Skill* purify = Factory::CreateSkill ( SKILLTYPE_PURIFY );
Skill* skills[2] = {disease, purify};
vector<Skill*> int_vector = Tools::MakeVector ( skills );
ofstream fileOut;
fileOut.open ( "data.txt", std::ofstream::binary );
ISerializable::SerializePVector( fileOut, int_vector );
fileOut.flush();
fileOut.close();
ifstream fileIn;
vector<Skill*> int_vector2;
fileIn.open ( "data.txt", std::ofstream::binary );
ISerializable::DeSerializePVector( fileIn, int_vector2 );
}
How am I supposed to get this work?
Assuming
ElementTypehas a default constructor, thenIt
ElementTypeis already a pointer type, then you may need this (C++11):If you don’t have C++11 support, you can use the
boostequivalent.