I have a superclass Element with multiple subclasses, let's call them A and B. I want to overload << and >> so I can save and load my objects.
class Element
{
public:
Element();
int superProperty;
virtual void write(iostream &out)
{
out << superProperty;
}
virtual void read(iostream &in)
{
in >> superProperty;
}
};
iostream operator<<(iostream &out, const Element &elt)
{
elt.write(out);
return(out);
}
iostream operator>>(iostream &in Element &elt)
{
elt.read(in);
return(in);
}
class A : public Element
{
public:
A();
int subProperty;
void write(iostream &out)
{
Element::write(out);
out << subProperty;
}
void read(iostream &in)
{
Element::read(in);
in >> subProperty;
}
};
class B : public Element
{
public:
B();
double subProperty;
void write(iostream &out)
{
Element::write(out);
out << subProperty;
}
void read(iostream &in)
{
Element::read(in);
in >> subProperty;
}
};
With these definitions, I can easily write out a file of my Elements, writing each one as
iostream mystream;
Element e;
...
mystream << e;
Where I'm stuck is reading them back in. I want it to look like this:
iostream mystream;
Element *pe;
...
pe = new Element(); // the problem is right here
mystream >> *pe;
But that won't work because I don't know if the element I'm about to read is an Element or an A or a B. (In my application, I never actually instantiate an Element. All objects are one of the subclasses.) I resorted to writing out a char to indicate the class...
if (dynamic_cast<A>(e))
{
out << 'A';
out << e;
} else if (dynamic_cast<B>(e))
{
out << 'B';
out << e;
}
and then switch/casing to read like this:
char t;
Element *pe;
...
in >> t;
switch (t)
{
case 'A':
pe = new A;
break;
case 'B':
pe = new B;
break;
}
in >> *pe;
but it seems inelegant.
What is a better way to stream my disparate objects?