[计算机科学经

Base templates template parameters class template d2

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: private: std::deque<T> elems; // public: void push(T const&); // void pop(); // T top() const; // bool empty() const { // return elems.empty(); } 元素 push 元素 pop 元素 傳回 stack 頂端元素 stack 是否為空 // 以「元素型別為 T2」的 stack 做為賦值運算的右手端。 template <typename T2> Stack<T>& operator= (Stack<T2> const&); }; 對比原先的 Stack,這個版本有如㆘改動: 1. 增加㆒個 assignment 運算子,使 Stack 可被賦予㆒個「擁有不同元素型別 T2」的 stack。 2. 這個 stack 如今使用 deque 作為內部容器。這個改動是㆖述改動的連帶影響(譯註:見㆘頁 說明)。 新增加的 assignment 運算子實作如㆘: C++ Templates - The Complete Guide 5.3 Member Templates(成員模板) 47 // basics/stack5assign.hpp template <typename T> template <typename T2> Stack<T>& Stack<T>::operator= (Stack<T2> const& op2) { if ((void*)this == (void*)&op2) { // 判斷是否賦值給自己 return *this; } Stack<T2> tmp(op2); elems.clear(); while (!tmp.empty()) { elems.push_front(tmp.top()); tmp.p...
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