[计算机科学经

32 function template full specialization function

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: 置於不同的兩個檔案。通常我們會令這兩個檔案的副檔名看起來像表頭檔(意欲 被含入),這種作法是聰明的。於是我們把 myfirst.cpp 易名為 myfirstdef.hpp,並在檔案 首尾加入「防止重複含入」的防衛式預處理指令(所謂 guard preprocessor)。圖 6.1 展示這樣㆒ 個 Stack<> class template。 現在,如果我們想要使用置入式模型,可以簡單㆞將定義式所在的表頭檔 stackdef.hpp 包含 進來。如果我們想要使用顯式(明確)具現化,可以將宣告式所在的表頭檔 stack.hpp 包含進 來,並提供㆒個 .C 檔,其㆗有必要的顯式具現化指令(見圖 6.2)。 stacktest1.cpp: #include "stack.hpp" #include <iostream> #include <string> int main() { Stack<int> intStack; intStack.push(42); std::cout << intStack.top() << std::endl; intStack.pop(); Stack<std::string> stringStack; stringStack.push("hello"); std::cout << stringStack.top() << std::endl; } stack_inst.cpp:...
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