Serializing a pointer array with a twist 
Author Message
 Serializing a pointer array with a twist

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.*-*-*.com/
Before you buy.



Sun, 20 Apr 2003 21:25:30 GMT  
 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.



Sun, 20 Apr 2003 23:31:40 GMT  
 Serializing a pointer array with a twist
Thankyou Robert for your reply.

Quote:

> I had a quick look at this .. the problem is your macros. Have you
looked to
> see what these macros do ?

I had a little look at the definitions and even tried putting
XXX_DYNCREATE into CFruit. However, I had little idea of what I was
doing so my uneducated guesses were ignored and I looked for other
things that could be the problem.

When I put XXX_DYNAMIC into CFruit it compiled fine. As an added bonus,
I made a closer examination of the macros and worked out what they
actually do. :)

Now to work out how to make those serialized pointers behave...

Rodney Lorrimar

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 21 Apr 2003 08:30:01 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Pointers & matrices: Twist on the original question

2. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

3. Array of pointers, pointer to array...

4. array pointer/pointer array

5. arrays pointers array of pointers

6. Pointer of Pointers was Pointer of arrays...

7. Pointers: return of pointer to array of pointers to main

8. Serialize to byte[] array

9. serialize a collection as an array object

10. serializing pointers between two processes

11. serialized arrays are they copied or shared?

12. pointers to an array and accesing elements of the array

 

 
Powered by phpBB® Forum Software