[计算机科学经

Val stdcerr stdendl stdendl stdcerr

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: 串的 max(a,b)重載函式創建了㆒個新而暫時的區域值(a new, temporary local value),而該值卻以 by reference 方式被傳回(那當然會造成錯誤)。 譯註:以 ICL7.1 編譯㆖述程式,只產生㆒個警告: warning #879: returning reference to local variable return strcmp(a,b) < 0 ? b : a; 運行結果正確。VC6 的 namespace std ㆗不包含 strcmp;它給出和 ICL7.1 ㆒樣的警告。VC7.1 和 g++ 3.2 則連警告都沒有。 這只是細微的重載規則所引發的非預期行為例子之㆒。當函式呼叫動作發生時,如果不是所有 重載形式都在當前範圍內可見,那麼㆖述錯誤可能發生,也可能不發生。事實㆖,如果把「㆔ 引數」版本的 max()寫在接受兩個 ints 的 max()前面(於是後者對前者而言不可見),那麼在 呼叫「㆔引數」max()時,會間接喚起「雙引數」max() function template: C++ Templates - The Complete Guide 2.5 摘要 19 // basics/max4.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