-
Notifications
You must be signed in to change notification settings - Fork 15
Base Class
jacob-carlborg edited this page Mar 1, 2013
·
4 revisions
This example shows how to serialize a subclass through a base class reference.
module main;
import std.stdio;
import orange.serialization._;
import orange.serialization.archives._;
class Base
{
int a;
int getA ()
{
return a;
}
int getB ()
{
return a;
}
}
class Sub : Base
{
int b;
override int getB ()
{
return b;
}
}
void main()
{
auto archive = new XmlArchive!(char);
auto serializer = new Serializer(archive);
Serializer.register!(Sub); // Register the subclass with the serializer
auto sub = new Sub;
sub.a = 3;
sub.b = 4;
// Change the static type to Base
Base base = sub;
serializer.serialize(base);
// Deserialize the serialized data as the type of the base class
auto b = serializer.deserialize!(Base)(archive.untypedData);
// Verify that the deserialized values are equal to the original values.
// Here we uses virtual methods to make sure we're accessing the value
// of the subclass.
assert(sub.a == b.getA());
assert(sub.b == b.getB());
}