我在使用 Marmalade SDK 进行序列化时遇到问题。我了解序列化原语之类的原则,但是多态对象呢?
根据 Marmalade 示例,您可以像这样序列化对象:
void Serialise()
{
uint8 isObject;
if (IwSerialiseIsWriting())
// writing
{
// record whether m_Happy exists
isObject = m_Happy ? 1 : 0;
IwSerialiseUInt8(isObject);
// serialise out m_Happy if it exists
if (m_Happy)
m_Happy->Serialise();
}
else
// reading
{
IwSerialiseUInt8(isObject);
// if an object was written out then read it in
if (isObject)
{
// if m_Happy already exists then delete it
if (m_Happy)
delete m_Happy;
m_Happy = new CHappy;
// serialise in m_Happy
m_Happy->Serialise();
}
}
}
我的问题是当你有一个多态类型时如何使用这个方法?例如:
我有一个对象:
AbstractClass *object;
序列化时,如何正确启动此对象?根据简单的果酱示例,我会:
// reading
if(objectExists) {
object = new AbstractClass(); // uh oh
}