Re: How to serialize Mfc template dialog class ?
On Sep 4, 11:53 am, Elizabeta <Elizab...@discussions.microsoft.com>
wrote:
If your dialog is used to entry/show some data, you may want to create =
a
C++ class derived from CObject that stores that data (e.g. a CPerson,
with name, surname, address, etc.), and use DECLARE_SERIAL and
IMPLEMENT_SERIAL on this non-GUI class.
Ok lets say that for Dialog template classes I can do your suggestion, th=
anks.
But what about template classes that are not dialogs, so I want to
reformulate my question :
How to use macros like DECLARE_SERIAL and IMPLEMENT_SERIAL with c++ templ=
ate
classes derived from CObject ?
You really can't do that. You can only serialize a template
instantiation. So, for example, you could do:
template<typename T>
class C : public CObject
{
void Serialize(CArchive& ar) { ar>>member, ar>>member; }
T member;
};
then
class CInt : public CObject<int>
{ DECLARE_SERIAL(CInt) }
IMPLEMENT_SERIAL(CDouble, CObject, schema...)
class CDouble : public CObject<double>
{ DECLARE_SERIAL(CDouble) }
IMPLEMENT_SERIAL(CDouble, CObject, schema...)
So...
1. you must have a concrete class for XXX_SERIAL macros to even
compile
2. if you ever change template type (e.g. from int to short), you must
change the schema number and deal with that. This may be tricky for a
beginner ;-)
Also, to be able to serialize a class (e.g a template), you don't
necessarily need to use XXX_SERIAL macros, but then:
1. you can't use schema number for format evolution
2. >> and << operators don't work (consequence: you can't serialize
using CObArray).
What Joe says about schema migration being impossible is not true.
It's not easy, but not impossible. You must know how-tos and pitfalls,
but it's doable. For example, I have code here at work that can read
stuff serialized more than a decade ago, and that has since undergone
literally thousands of changes in saved data. There are constraints,
but it is very rare that we break serialization. If fact,
serialization problems are a tiny minority compared to other issues.
HTH,
Goran.