Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Serialize of derived classes
Re: Serialize of derived classes [message #36358 is a reply to message #36357] Tue, 22 May 2012 13:08 Go to previous messageGo to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
koldo wrote on Tue, 22 May 2012 05:15

Hello all

By doing the enclosed code MyClass data is serialized but MyClassA is not used.

How it has to be done to store the data of MyClassA including the data of the parent MyClass?

struct MyClass {
	MyClass(int data) : data(data) {};
	int data;
	void Serialize(Stream& stream) 	{stream % data;}
};

struct MyClassA : public MyClass {
	MyClassA(int dataA) : dataA(dataA) {data = dataA*2;};
	int dataA;
	void Serialize(Stream& stream) 	{stream % dataA; MyClass::Serialize(stream);}
}

struct MyClassList {
	Array<MyClass> elems;
	void Serialize(Stream& stream) 	{stream % elems;}
};

...
elems.Add(new MyClassA(12));
StoreToFile(elems, "myfile");



This code is fundamentally wrong - elems can contain both MyClassA and MyClassB, means you need a class factory when loading the file (and then perhaps virtual Serialize).

In other words, Array::Serialize does not support polymorphic content...
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Value: String to int - problem or feature?
Next Topic: Problem with GetTickCount on Linux
Goto Forum:
  


Current Time: Wed May 08 10:59:02 CEST 2024

Total time taken to generate the page: 0.02208 seconds