Re: Enum serialization
enum is a "short" type, so all you have to do is cast your enum value to
short while serializing, and from short to enum while deserializing.
if (ar.IsStoring())
ar << (short)m_element;
else
{
short value;
ar >> value;
m_element = (Elementi)value;
}
You can also add an overloaded << or >> operator to your class that is
derived from CArchive:
class CMyArchive : public CArchive
{
public:
CMyArchive& operator << ( const Elementi& value )
{
CArchive::operator << ((short)value);
return *this;
}
CMyArchive& operator >> ( Elementi& value )
{
short shValue;
CArchive::operator >> (shValue);
value = (Elementi)shValue;
return *this;
}
};
redefine the document's Serialize() as
void CMyDoc::Serialize( CMyArchive& ar )
But this looks like a hassle...
"Marco Biagioni" <marco1036@interfree.it> wrote in message
news:AF23362B-3C33-451E-90C8-8CBD4E30AB33@microsoft.com...
I take an error trying serialize a member in document class of type enum...
binary '>>' : no operator found which takes a right-hand operand of type
'Elementi' (or there is no acceptable conversion)
where i defined
enum Elementi{Line,Rectangles,Circle,Curve,TEXT};
enum Elementi m_element;
and for example m_element = Line;
so in serialize doc function i have in my serialize function...
if (ar.IsStoring())
{
ar << m_color // Store the current color
<< m_element // the current element type, !!!THIS LINE GIVES NO ERROR!!!
<< m_PenWidth // and the current pen width
<< m_DocSize; // and the current document size
}
else
{
ar >> m_color // Retrieve the current color
m_element // the current element type, !!!ERROR IN THIS LINE!!!
m_PenWidth // and the current pen width
m_DocSize; // and the current document size
Maybe i should implement a personalized operator overload for >> ?
And how should i implement that?And where?
Why << operator doesn't give me the error and works fine?