假设我有这样的结构:
class AAA
{
BBB bb_member;
double dbl_member;
....................
}
class BBB
{
int int_member;
QString QStr_member;
.................
QTextEdit m_textEdit;
}
对于 AAA,我定义了这个运算符:
QDataStream &operator<<(QDataStream &out, const AAA &aa)
{
out << aa.bb_member
<< aa.dbl_member;
return out;
}
QDataStream &operator>>(QDataStream &in, AAA &aa)
{
BBB bb_memb;
double dbk_memb;
in >> bb_memb
>> dbk_memb;
aa = AAA(bb_memb, dbk_memb);
return in;
}
然后我称之为:
QFile file("myFileName");
file.open(QIODevice::WriteOnly))
QDataStream out(&file);
out << AAA_object;
为了将 AAA 对象序列化为文件。
现在的问题。我应该如何为 BBB 类定义 QDataStream 运算符以序列化 BBB 对象(int、QString 和QTextEdit 到达文本内容),同时调用<< AAA_object; ???