Since my preliminary question seemed confusing, I think it is best to clearly state what I want to achieve.
I have (ignore inheritance now and focus on X):
class Base {};
class X : public Base {
private:
double m_double;
public:
template<class A> friend
void state( A& a, const X& x ) {
data( a, x.m_double, "m_double" );
}
};
Now I can introduce arbitrary new classes that perform different actions depending on how the data function is overloaded, we will refer to them as Accessors in the following:
class XmlArchive {...};
template<class T>
void data( XmlArchive& a, const T& t, const std::string& str ) {
}
class ParameterList {...};
template<class T>
void data( ParameterList& a, const T& t, const std::string& str ) {
}
Then I can write:
X myX;
XmlArchive myArchive;
state( myArchive, myX );
ParameterList myParameters;
state( myParameters, myX );
Fantastic, code reuse !: D However, the following (explicitly) fails:
Base* basePtr = new X;
state( myParameters, *basePtr );
The goal is to make this last challenge a success. What I reviewed (and why this is unacceptable):
First option: make all Accessors inherit from a common base class, such as AccessorBase, and then write to Base
virtual state( AccessorBase& a ) const = 0;
X ( , ).
, AccessorBase , (-) . (. , X, Y ), , .
: Accessor. open/close, Accessor (, TxtArchive) .
, - ( vtbls ).
, ... , X, Accessor , ( XmlArchive):
state( xmlArchive, x ); //xmlArchive of type XmlArchive, x of type X
.
, :
state( myParameters, *basePtr );
basePtr , , .
, boost:: serialize - , , (, ++ , This(), , ...)
!