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 » Newbie corner » How to access base class function in this case?
How to access base class function in this case? [message #55331] Sat, 31 October 2020 14:05 Go to next message
sinpeople is currently offline  sinpeople
Messages: 29
Registered: October 2020
Location: Singapore
Promising Member

Hi folks,

I am studying the binary serialization in an inheritance hierarchy. In the code below, it seems the serialization methods (both Serialize() and ToString()) of the base class is never called. Is there any way to call it?
I want the serialization of an extended class also includes its immediate user-defined base class.

Thank you so much!

#include <Core/Core.h>

using namespace Upp;

struct Foo {
	int         q;
	String      text;
	Vector<int> data;

	void Serialize(Stream& s)
	{
		s % q % text % data;
	}

	String ToString() const
	{
		String s;
		s << "q = " << q << ", text = \"" << text << "\", data:";
		for(int i = 0; i < data.GetCount(); i++)
			s << ' ' << data[i];
		return s;
	}

	Foo() { q = 0; }
};

struct FooExt : Foo
{
	int nMessageID;
	VectorMap<String, int> scores;

	void Serialize(Stream& s)
	{
		s % nMessageID % scores;	
	}
	
	String ToString() const
	{
		String s;
		s << "nMessageID = " << nMessageID;
		for(int i = 0; i < scores.GetCount(); i++)
		{
			s << scores.GetKey(i) << ' ' << scores[i];
		}
		
		return s;
	}
	FooExt() { q = 0; }
};

CONSOLE_APP_MAIN
{
	FooExt foo;
	LoadFromFile(foo, ConfigFile("data.bin"));
	Cout() << "Loaded Foo: " << foo << '\n';
	foo.text = FormatIntRoman(foo.q);
	foo.q++;
	foo.data.Add(foo.q);
	foo.nMessageID = 1001;
	foo.scores.Add("Jack", 95);
	foo.scores.Add("Joe", 100);
	StoreToFile(foo, ConfigFile("data.bin"));
	Cout() << "Saved Foo: " << foo << '\n';
}





Best Regards
David
Re: How to access base class function in this case? [message #55332 is a reply to message #55331] Sat, 31 October 2020 14:39 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member

struct FooExt : Foo
{
	int nMessageID;
	VectorMap<String, int> scores;

	void Serialize(Stream& s)
	{
		Foo::Serialize(s); // <<<<<<<<<<<<<<<<<<< ------------------
		s % nMessageID % scores;	
	}
Previous Topic: What collection to use to achieve this?
Next Topic: login and main window
Goto Forum:
  


Current Time: Fri Mar 29 11:01:55 CET 2024

Total time taken to generate the page: 0.01067 seconds