///////////////////////////////////////////////////////////////// // PTS.cpp - Issue #4 : partial template specialization // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2005 // ///////////////////////////////////////////////////////////////// // Partial Template Specialization: // Allows you to specialize a subset of a class's template // parameters. // // Below we give an example of a smart pointer designed // to handle arrays of some fixed size, determined by the // integer template parameter. // // For non-arrays we specialize the template for size=1, // leaving the type parameter T unspecified. #include #include template class smartPtr { public: smartPtr(T* pTr = 0) { pTr_ = new T[size]; if(pTr) for(int i=0; i class smartPtr { public: smartPtr(T* pTr) : pTr_(new T(*pTr)) {} ~smartPtr() { delete pTr_; } T& operator*() { return *pTr_; } // note that operator[] is not supplied // other members private: T* pTr_; }; // void main() { std::cout << "\n Demonstrating Partial Template Specialization"; std::cout << "\n ===============================================\n"; smartPtr spArray; spArray[0] = "first string"; spArray[1] = "second string"; spArray[2] = "third string"; for(int i=0; i<3; ++i) std::cout << "\n " << spArray[i]; std::cout << "\n"; std::string s2 = "another string"; smartPtr spNoArray(&s2); std::cout << "\n " << *spNoArray; std::cout << "\n\n"; }