Hi guys & girls,
I do have the following question. Let's say I have a skeleton for some functionality, which I want to implement in different ways. One way is the Template pattern, but that uses a polymorphic call to my (protected) implementation function.
Instead, I want to use a static interface using the Barton-Nackman trick. But then, if I want to store a list of these algorithm implementations in some algorithm-container class, i'm in trouble. Do I not escape from a heterogenous container with "boost::any" ?
Here's my code :
Thanks for suggestions !Code:template<typename Type> class Document { public: virtual ~Document(){} void open() { read(); } void save() { } void close() { } protected: Type& read() { return static_cast<Type&>(this)->read(); } }; struct latex : public Document<latex> { void read(){ } }; class Application { public: template<typename Type> void addDocument() { document_ = new T; } // suppose T has default constructor and is POD ~Application() { delete document_; } // don't bother the raw pointer.. void OpenDocument() { document_->open(); } protected: Document* document_; }; int main() { Application emacs; emacs.addDocument<latex>(); emacs.OpenDocument(); return 0; }
Mark
ps : is this the BN trick? or is this already a crtp.. i have the impression that a crtp is merely the use of the BN trick in template expressions



LinkBack URL
About LinkBacks


