Listing 2: Adding an extra template parameter with a default value.
class FreeDefaultContext;
class InValueContainer;
template <typename T, class Context = FreeDefaultContext>
class DynObj: private Context {
protected:
DynObj(T* dao): _dao(dao) {}
~DynObj() {delete _dao;}
typedef DynObj<T,InValueContainer> InValueContainer;
typedef DynObj<T,FreeDefaultContext> FreeDefaultContext;
protected: // now it's up to Context to decide
DynObj(const DynObj&);
void operator=(const DynObj&);
private:
T* _dao; // the DAO we represent
};
class FreeDefaultContext {
public:
bool soleOwner() const {return true;}
private: // forbid copy construction and assign
FreeDefaultContext(const FreeDefaultContext&);
void operator=(const FreeDefaultContext&);
};