通常除了从CObject继承的类重写Serialize外,我们经常还遇到对自定义Struct, enum等的序列化,这里就说说后两个的可以方法:
一、结构体:
struct A
{
int a ;
float b;
}structA;
1 最简单却较繁琐的方法:
ar<<stuctA.a<<stuctA.b
2 为结构体添加 重载符:
struct A{
int a ;
float b;
friend CArchive & operator <<(CArchive & arc,A &data);
friend CArchive & operator >>(CArchive & arc,A &data);
}
CArchive & operator <<(CArchive & arc,A &data)
{
arc<<a<<b;
return arc;
}
CArchive & operator >>(CArchive & arc,A &data)
{
arc>>a>>b
return arc;
}
二、枚举类型:
enum Shape { rectangle, roundRectangle, ellipse,line,circle,picture,text,polygon };
Shape m_nShape;
ar << (WORD) m_nShape;
WORD wTemp1;
ar >> wTemp1; m_nShape = (Shape)wTemp1;
一、结构体:
struct A
{
int a ;
float b;
}structA;
1 最简单却较繁琐的方法:
ar<<stuctA.a<<stuctA.b
2 为结构体添加 重载符:
struct A{
int a ;
float b;
friend CArchive & operator <<(CArchive & arc,A &data);
friend CArchive & operator >>(CArchive & arc,A &data);
}
CArchive & operator <<(CArchive & arc,A &data)
{
arc<<a<<b;
return arc;
}
CArchive & operator >>(CArchive & arc,A &data)
{
arc>>a>>b
return arc;
}
二、枚举类型:
enum Shape { rectangle, roundRectangle, ellipse,line,circle,picture,text,polygon };
Shape m_nShape;
ar << (WORD) m_nShape;
WORD wTemp1;
ar >> wTemp1; m_nShape = (Shape)wTemp1;