[计算机科学经

计算机科学经

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: // return elems.empty(); } push 元素 pop 元素 傳回 stack 的頂端元素 stack 是否為空 // 以元素型別為 T2 的 stack 賦值 template <typename T2, typename CONT2> Stack<T,CONT>& operator= (Stack<T2,CONT2> const&); }; 此時的 template assignment 運算子可實作如㆘: C++ Templates - The Complete Guide 5.3 Member Templates(成員模板) 49 // basics/stack6assign.hpp template <typename T, typename CONT> template <typename T2, typename CONT2> Stack<T,CONT>& Stack<T,CONT>::operator= (Stack<T2,CONT2> const& op2) { if ((void*)this == (void*)&op2) { // 判斷是否賦值給自己 return *this; // 譯註:請見 p47 之「英文版勘誤」 } Stack<T2,CONT2> tmp(op2); elems.clear(); while (!tmp.empty()) { elems.push_front(tmp.top()); tmp.pop(); } return *this; // 創建 op2 的㆒份拷貝 // 移除所有現有元素 // 複製所有元素 } //譯註:VC6 無法編譯㆖例的 noninline operator=。VC7.1/ICL7.1/g++ 3.2 無此問題。 記住,對 class templates 而言,只有「實際被呼叫的成員函式」才會被具現化。因此如果你不至 於令不同(元素)型別的 stacks 彼此賦值,那麼甚至可以拿 vector 當作內部元素的容器(譯註: 而先...
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