Seems to me you hit exactly the same problem I did: you can't define an
operator<< within a class to serialise an arbitrary type, only to
serialise the containing class (and on further experimentation I can't
see what use it is, you have to write object << std::cout)
But you've given me an idea...
struct A {
std::string to_string() const { std::ostringstream s; s <<
"this A is at " << this; return s.str();}
};
struct XML {
struct XML_A: public A {
} a;
};
std::ostream& operator<<(std::ostream& os, XML::XML_A const & a) { os <<
"<A>" << a.to_string() << "</A>" ; return os;}
std::ostream& operator<<(std::ostream& os, XML const & x){ os << x.a;
return os;}
struct JSON {
struct JSON_A: public A {
} a,b;
};
std::ostream& operator<<(std::ostream& os, JSON::JSON_A const & a) { os
<< "{\"A\":\"" << a.to_string() << "\"}"; return os;}
std::ostream& operator<<(std::ostream& os, JSON const & j){ os << j.a <<
j.b; return os;}
Darnn it's ugly.
Andy