#include <iostream> using namespace std; // Safe Delete Pointer #define SAFEDELETE(p) if (NULL != p)/ {/ delete p;/ p = NULL;/ } // base product class ProductBase { public: ProductBase(void) { } virtual void print() { } }; // sub product class ProductInt :public ProductBase { public: ProductInt(void) { } void print() { cout<<"this is print integer Num/n"<<endl; } }; // sub product class ProductFloat :public ProductBase { public: ProductFloat(void) { } void print() { cout<<"this is print float Num/n"<<endl; } }; // product creator template<typename TheProduct> class CreatorBase { public: CreatorBase(void) { Product = NULL; } virtual ~CreatorBase(void) { if (NULL != Product) { delete Product; Product = NULL; } } virtual void CreateProduct() { SAFEDELETE(Product); Product = new TheProduct(); } virtual void print() { if (NULL == Product) { CreateProduct(); } Product->print(); } protected: ProductBase* Product; }; //class CreateIntProduct :public CreatorBase //{ //public: // CreateIntProduct(void) // { // // } // // void CreateProduct() // { // Product = new ProductInt(); // // new Other Product // // ... // } //}; // //class CreateFloatProduct :public CreatorBase //{ //public: // CreateFloatProduct(void) // { // // } // void CreateProduct() // { // Product = new ProductFloat(); // } //}; int main() { CreatorBase<ProductInt> CreatorInt; CreatorInt.print(); CreatorBase<ProductFloat> CreatorFloat; CreatorFloat.print(); getchar(); return 0; }