totem
16th January 2011, 11:55
I have an inheritance tree like :
BaseClass : QObject {
Q_OBJECT
// etc..
} ;
ChildClass1 : BaseClass {
Q_OBJECT
// etc..
} ;
ChildClass2 : BaseClass {
Q_OBJECT
// etc..
} ;
And for each class I declared and defined default constructor, copy constructor, assignment operator overload, and friend << and >> operators for QDataStream support.
Now, I have a list of BaseClass pointers used as follow :
void saveData(QDataStream & stream)
{
QList<BaseClass*> objList ;
objList.append( new ChildClass1() ) ;
objList.append( new ChildClass2() ) ;
stream << objList.length() ;
foreach(BaseClass *obj, objList)
{
stream << (*obj) ;
}
}
So when I serialize this list, how do I make sure it's actually ChildClass1 or ChildClass2 instances beeing serialized ? stream operators are non-member functions, so they cannot be virtual.
The same way when I de-serialize objects, (I must be blind but) I'm stuck with the following :
void readData(QDataStream & stream)
{
int i, count ;
QList<BaseClass*> someList ;
stream >> count ;
for(i=0; i<count; i++)
{
stream >> ...// now what do I do here ?
}
}
Long story short, how do I combine Qt serialization and class inheritance ?
Any pointer would be greatly appreciated
BaseClass : QObject {
Q_OBJECT
// etc..
} ;
ChildClass1 : BaseClass {
Q_OBJECT
// etc..
} ;
ChildClass2 : BaseClass {
Q_OBJECT
// etc..
} ;
And for each class I declared and defined default constructor, copy constructor, assignment operator overload, and friend << and >> operators for QDataStream support.
Now, I have a list of BaseClass pointers used as follow :
void saveData(QDataStream & stream)
{
QList<BaseClass*> objList ;
objList.append( new ChildClass1() ) ;
objList.append( new ChildClass2() ) ;
stream << objList.length() ;
foreach(BaseClass *obj, objList)
{
stream << (*obj) ;
}
}
So when I serialize this list, how do I make sure it's actually ChildClass1 or ChildClass2 instances beeing serialized ? stream operators are non-member functions, so they cannot be virtual.
The same way when I de-serialize objects, (I must be blind but) I'm stuck with the following :
void readData(QDataStream & stream)
{
int i, count ;
QList<BaseClass*> someList ;
stream >> count ;
for(i=0; i<count; i++)
{
stream >> ...// now what do I do here ?
}
}
Long story short, how do I combine Qt serialization and class inheritance ?
Any pointer would be greatly appreciated