
Serializing a pointer array with a twist
I had a quick look at this .. the problem is your macros. Have you looked to
see what these macros do ?
XXX_DYNAMIC allows RunTime information
XXX_DYNCREATE does XXX_DYNAMIC and allows dynamic creation through
CreateObject
XXX_SERIAL DOES xxx_DYNCREATE and overloads the << and >> operators
you declare your CFruit to be XXX_SERIAL so the compiler generates
XXX_SERIAL is a superset of XXX_DYNCREATE
so what does IMPLEMENT_DYNCREATE do ?
#define IMPLEMENT_DYNCREATE(class_name, base_class_name) \
CObject* Pascal class_name::CreateObject() \
{ return new class_name; } \
Wow, your compiler has generated
CFruit *PASCAL CFruit::CreateObject()
{ return new CFruit; }
No can do ... CFruit is a virtual base class.
How do you get around this ? Well a quick ( but very educated ) guess
DECLARE_DYNAMIC / IMPLEMENT_DYNAMIC your CFruit class - that gets rid of
the 'new CFruit' code generation but allows RTTI. Then with your derived
classes CApple, CBanana etc. You need to DECLARE_SERIAL / IMPLEMENT_SERIAL.
Hope it works.
Quote:
> I've searched Deja for information regarding this but didn't find any
> info specific to my problem.
> The problem is that I am serializing a CArray of pointers to CObject
> derived objects and Things Don't Work.
> What sets this particular issue apart from others that I have read about
> in the group archives, various FAQ's and the MFC documentation is that
> my object array is defined like this:
> CArray<CFruit*, CFruit*> BagOfFruit;
> and that CFruit is an abstract base class derived from CObject. The
> members of BagOfFruit point to things such as CBanana, CApple or
> CKiwiFruit, which are all derived from CFruit.
> However, if I put the DECLARE_SERIAL and IMPLEMENT_SERIAL macros in
> their proper places in CFruit's class definition and implementation
> files, things go decidedly pear-shaped, so to speak. The compiler
> complains like this:
> error C2259: 'CFruit' : cannot instantiate abstract class due to
> following members:
> warning C4259: 'class CFruit *CFruit::Clone(void)' : pure virtual
> function was not defined
> warning C4259: 'void CFruit::Draw(class CDC*, bool)' : pure virtual
> function was not defined
> The problem is that being an abstract base class is a requirement by
> design for CFruit. A CFruit object should not exist.
> If I comment out these macros in CFruit the compiler complains about the
> ?_SERIAL macros in CApple, CBanana, etc, saying:
> error C2039: 'classCFruit' : is not a member of 'CFruit'
> error C2065: 'classCFruit' : undeclared identifier
> All of my objects have the correct Serialize(CArchive& ar) functionality
> and I've written a SerializeElements functions for BagOfFruit.
> Has anyone got any ideas of what's my problem?
> Thankyou,
> Rodney Lorrimar
> Sent via Deja.com http://www.deja.com/
> Before you buy.