Serialization Question
Kalyan -- chakri@sunserv.cmc.stph.net
Monday, October 14, 1996
Environment : Win95, VC++ 4.2
Hi all,
I have two classes like this :
class CBase : public CObject
{
protected:
int a;
public :
virtual void Serialize(CArchive& ar);
int getA();
};
class CDerived : protected CBase
{
public :
void SetA(int);
virtual void Serialize(CArchive& ar);
};
I an application say App1, I use the class CDerived to declare
objects and use the setA function to set the attribute 'a' and then
serialize them to an archive (file). And in other application say App2 I
declare objects of CBase so that those object's attribute can only
be read using getA function and these objects are serialized(read) from the
same archive(file) as that of App1; Will this approach workout?
Any suggestions would be appreciated.
Thanx in advance,
Kalyan.V
ganeshs@nationwide.com
Tuesday, October 15, 1996
[Mini-digest: 4 responses]
Environment : Win95, VC++ 4.2
> Hi all,
> I have two classes like this :
>
> class CBase : public CObject
> {
> protected:
> int a;
>
> public :
> virtual void Serialize(CArchive& ar);
> int getA();
> };
>
> class CDerived : protected CBase
> {
> public :
> void SetA(int);
> virtual void Serialize(CArchive& ar);
> };
>
> I an application say App1, I use the class CDerived to declare
> objects and use the setA function to set the attribute 'a' and then
> serialize them to an archive (file). And in other application say App2 I
> declare objects of CBase so that those object's attribute can only
> be read using getA function and these objects are serialized(read) from
> the
> same archive(file) as that of App1; Will this approach workout?
>
>
> Any suggestions would be appreciated.
>
> Thanx in advance,
> Kalyan.V
As long as CBase and CDerived are serializable (Basically the
CRuntimeClass information needs to be in the class registry, which
happens even if you use the DYNCREATE macros. To do something like
ar << pDerived
in App1 of course, you would need to have the SERIALIZE macros for
CDerived as well ...), there's no reason why something like
ar >> pBase;
shouldn't work in App2...
/ ___| / ___| __ _ _ __ ___ ___| | I do not speak for
\___ \ | | _ / _` | '_ \ / _ \/ __| '_ \ Tata Unisys or
___) | | |_| | (_| | | | | __/\__ \ | | |Nationwide Ins.
|____(_) \____|\__,_|_| |_|\___||___/_| |_|------------------
-----From: rkumar@mail.cswl.com
It all depends on how u implement the serilization routines.
I guess the derived class dose not hava any persistant attributes
in that case there is no need for a serialise function in the derived
class U can use the serialization routine of the base class for both
classes.
Ratan
Rkumar@cswl.com
-----From: CraigTT@ccmail01.PE-Nelson.COM
Kalyan,
Since you're not adding any variables, it should work IF you serialize
via a direct call to Serialize.
If you serialize via pointers through the insert and extract
operators, hit the problem of the runtime class information being
written and it won't match between the two instances. You can
probably get around this if the writing app casts the CDerived
pointers to CBase before using the insert operator. If the writing
app also needs to read the archive it would have to use a temporary
variable of CBase* to read each item from the archive and then use a
downcast to assign it to a CDerived pointer.
Tim Craig
-----From: Mike Kurtinitis
Hi Kalyan,
I would suggest trying it out on two test apps. You can use App Wizard to
bang out two generic apps, then put the interesting code in OnNewDocument().
I would bet that it'll would work, but there's nothing like real-world testing!
Good luck,
-Mike
| Вернуться в корень Архива
|