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 » Community » PR, media coverage, articles and documentation » Some Shocking Facts You Probably Didn't Want to Know about U++ Framework...
Re: Some Shocking Facts You Probably Didn't Want to Know about U++ Framework... [message #55424 is a reply to message #55420] Mon, 09 November 2020 22:05 Go to previous messageGo to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Tom1 wrote on Mon, 09 November 2020 20:46
Hi Mirek!

Thanks!! Great article!

Although I started learning U++ 2006, I'm still learning... : An Array can actually hold objects of inherited classes! This was interesting and very useful for my needs. However, I could not figure out how to make serialization/deserialization of this work correctly. Here's my failed test of that:

using namespace Upp;
class A{
public:
	int	a;
	A(){
		a=1;
		Cout() << "A+\n";
	}
	
virtual	~A(){
		Cout() << "A-\n";
	}
	
virtual void Serialize(Stream &s){
		s % a;
	}
	
virtual void Print(){
		Cout() << "A: " << a << "\n";
	}
};

class B: public A{
public:
	int	b;
	B(){
		b=2;
		Cout() << "B+\n";
	}
	
	~B(){
		Cout() << "B-\n";
	}

virtual void Serialize(Stream &s){
		A::Serialize(s);
		s % b;
	}

virtual void Print(){
		Cout() << "B: " << a << ", " << b << "\n";
	}
};


CONSOLE_APP_MAIN{
	Array<A> v;
	
	v.Create<A>();
	v.Create<B>();
	v.Create<B>();
	v.Create<A>();
	
	Cout() << "Printing array...\n";
	for(int i=0;i<v.GetCount();i++) v[i].Print();

	Cout() << "Storing to file...\n";
	StoreToFile(v,GetHomeDirFile("Test.bin"));
	Cout() << "Clearing...\n";

	v.Clear();

	Cout() << "Loading from file...\n";
	LoadFromFile(v,GetHomeDirFile("Test.bin"));
	Cout() << "Loaded from file...\n";
	
	Cout() << "Printing array...\n";
	for(int i=0;i<v.GetCount();i++) v[i].Print();
}


What is the correct way to do this?


Seriously, you cannot... Sorry that I might have created that impression.

I mean, you definitely can solve this, but not with out-of-box Array::Serialize. You need to provide your own serialization there and store the type on saving, load on loading.

In reality, this is not needed very often...

Mirek
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message icon10.gif
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: Layout Designer has been documented (TheIDE)
Next Topic: 2nd price!
Goto Forum:
  


Current Time: Thu Mar 28 23:06:07 CET 2024

Total time taken to generate the page: 0.01908 seconds