Hello,
I am looking for suggestions on the following design (*). Basically
I am dealing with objects of type A, which can be serialized as 2
differents representations: B or C. I like this approach because B/C
can directly have access to the internal of A (which is required for
serialization). It also make it easy for user to add a new
representation D.
For the curious, I am writing a file format library and I am trying
to deal with third-party implementation bug. I know how to deal with
certain bugs, but they add a nonnegligable overhead if one is trying
to deal with them in the general case (B/C). Making them separate
classes make it easy for user to include/exclude them.
Thanks for comments,
-Mathieu
(*)
#include <iostream>
struct A
{
int a;
template <typename T>
void dump(std::ostream &os) {
static_cast<T&>(*this).dump(os);
}
};
struct B : public A
{
void dump(std::ostream& os) {
std::cout << "B:" << a << std::endl;
}
};
struct C : public A
{
void dump(std::ostream &os) {
std::cout << "C:" << a << std::endl;
}
};
int main()
{
A a = {2};
a.dump<B>(std::cout);
a.dump<C>(std::cout);
return 0;
}