[计算机科学经

2 template parameter 103 template template parameters

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: plate。 欲特化某個 class template,必須以 template<> 開頭宣告此㆒ class,後面跟著你希望的特化結 果。特化型別(specialized type)將作為 template arguments 並在 class 名稱之後直接寫明: template<> class Stack<std::string> { ... }; 對特化體(specializations)而言,每個成員函式都必須像常規的(㆒般的)成員函式那樣定義, 每㆒個 T 出現處都必須更換為特化型別(specialized type): void Stack<std::string>::push (std::string const& elem) { elems.push_back(elem); // 將傳入的 elem 附加於尾 } ㆘面是㆒個針對 std::string 型別而特化的 Stack<> 的完整範例: // basics/stack2.hpp #include <deque> C++ Templates - The Complete Guide 3 28 Class Templates(類別模板) #include <string> #include <stdexcept> #include "stack1.hpp" template<> class Stack<std::string> { private: std::deque<std::string> elems; // 元素 public: void push(std::string const&); void pop(); std::string top() const; bool empty() const { return elems.empty(); } }; // // // // push 元素 pop 元素 傳回 stack 最頂端元素 stack 是否為空 void Stack<std::string>::push (std::string const...
View Full Document

This note was uploaded on 08/25/2012 for the course EECS 101 taught by Professor Wre during the Fall '12 term at Beacon FL.

Ask a homework question - tutors are online