看板 CompBook 關於我們 聯絡資訊
RunPC《泛型指標與 Traits 技術》 文章勘誤 侯捷 [email protected] http://www.jjhou.com 此文亦將載於 2000/05 RunPC STL(4) 文章 ------------------------------------------------------------ 我於 2000/03 刊載於 Run!PC 雜誌上的《泛型指標與 Traits 技術》 (亦曾於三月下旬張貼於 BBS/News CompBook 版上)一文 ,其中有 個錯誤,經金興昇先生指正,十分感謝。 這個錯誤發生在 2000/03 Run!PC 雜誌 p226 中段程式碼。原碼如下: #01 template <class Node> // a tiny template wrapper class #02 struct node_wrap { #03 Node* ptr; #04 ... #05 Node& operator*() const { return *ptr; } #06 Node& operator->() const { return ptr; } #07 ... #08 node_wrap& operator++() { ptr = ptr->next; return *this; } #09 node_wrap& operator++(int) { node_wrap tmp = *this; ++*this; return tmp;} #10 ... #11 }; 應改為(注意 #06, #09): #01 template <class Node> // a tiny template wrapper class #02 struct node_wrap { #03 Node* ptr; #04 ... #05 Node& operator*() const { return *ptr; } #06 Node* operator->() const { return ptr; } #07 ... #08 node_wrap& operator++() { ptr = ptr->next; return *this; } #09 node_wrap operator++(int) { node_wrap tmp = *this; ++*this; return tmp;} #10 ... #11 }; 經過這樣的修改,p226 最下一行: node_wrap<int_node> r; // vc6[o] cb4[x] g++[o] 應為: node_wrap<int_node> r; // vc6[o] cb4[o] g++[o] ●心得: 原範例程式由於筆誤之故,應該是錯誤的,但 VC6 和 G++ 卻讓它通過。 那是因為程式中剛好都沒有呼叫那些錯誤的 member functions。 BCB4 和 BCB5 均拒絕讓它通過,可見 BCB 嚴謹的預防功夫。 -- the end  -- ※ Origin: 楓橋驛站<bbs.cs.nthu.edu.tw> ◆ Mail: [email protected]