作者ric2k1 (Ric)
看板EE_DSnP
標題Re: [問題] hw1.2.p2b
時間Tue Mar 17 21:31:42 2009
※ 引述《lovelylion2 (麻署鼠)》之銘言:
: 這題要用到function object的概念
: 教授在課堂上示範的example,原封不動丟進去compile,會出現以下警告:
: (我用 g++ -g -Wall -o 這些flag )
: fotest.cpp:6: 「class P」 有虛函式卻沒有虛解構函式
: fotest.cpp:12: 「class A」 有虛函式卻沒有虛解構函式
: fotest.cpp:18: 「class B」 有虛函式卻沒有虛解構函式
: 雖然只是警告,不過是什麼意思呢..?
Please add a "virtual destructor to class P". That is:
virtual ~P() {}
: -----
: 另外,題目中用selectionSort(arr, Less())來決定sort的順序
: 其中Less()是創造一個臨時的Less物件(呼叫default constructor,如果有的話)
: 然後傳給funtion嗎?
: 那,是不是function結束後就會把這個臨時的物件釋放掉呢?
Yes. 他會自動 destruct.
: 還有還有,selectionSort的prototype用
: void selectionSort(vector<int>& array, const Compare& compare);
: 而不是
: void selectionSort(vector<int>& array, const Compare compare);
: 是為了節省記憶體空間嗎?
因為對 selectionSort 而言沒有必要 copy 一份 Compare 的 object,
所以用 reference 傳入就好, 但因為不希望 function 去改它, 所以加 const.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.224.42.209