#include namespace AAA { class ISoul { public: ISoul(){} ClassDef(ISoul,0) }; namespace BBB { template class IConst : public AAA::ISoul { public: IConst() {} virtual IConst* Create( IConst* /*parent*/, T /*value*/ ) { return NULL; } virtual void Set( T /*value*/ ) {} ClassDef(IConst,0) }; template class Const; template class Value : public AAA::BBB::IConst { public: Value( Const* cnst ) : AAA::BBB::IConst() { fConst = cnst; } virtual IConst* Create( IConst* parent, T value ) { if( parent == NULL ) return fConst->Create(this,value); return fConst->Create(parent,value); } virtual void Set( T value ) { fValue = value; } protected: Const* fConst; T fValue; ClassDef(Value,0) }; template class Const : public AAA::BBB::IConst { typedef std::set< AAA::BBB::IConst * > ConstSet; typedef typename ConstSet::iterator ConstSetIterator; public: Const() : AAA::BBB::IConst() {} virtual IConst* Create( IConst* parent, T value ) { ConstSetIterator it = fConstValues.find( parent ); if( it != fConstValues.end() ){ return (*it); } IConst* cnst = new Value(this); cnst->Set( value ); fConstValues.insert( cnst ); return cnst; } protected: ConstSet fConstValues; ClassDef(Const,0) }; } } int main(){ AAA::BBB::Const* cnst = new AAA::BBB::Const(); AAA::ISoul* s = cnst->Create(NULL,33.3); }