我有一個通用的清單與模板通用列表中刪除非指針
template<class t>
class GenericList {
//the data is storeed in a chained list, this is not really important.
struct c_list { t data; c_list* next; ...constructor... };
public:
bool isDelete;
GenericList() : isDelete(false) {...}
void add(t d) {
c_list* tmp = new c_list(d, first->next);
//this is not really important again...
}
~GenericList() {
c_list* tmp = first;
c_list* tmp2;
while(tmp->next!=NULL) {
if (isDelete) { delete tmp->data; } //important part
tmp2=tmp->next;
delete tmp;
tmp=tmp2;
}
}
};
的重要組成部分,是isDelete
這僅僅是一個示例代碼
我需要這個,因爲我想要存儲這樣的數據:
GenericList<int> list;
list.add(22);list.add(33);
也
GenericList<string*> list;
list.add(new string("asd")); list.add(new string("watta"));
如果我儲存的問題只<int>
編譯器說,我不能刪除非指針變量,但我不希望在這種情況下。我該如何解決這個問題?
當我存儲<int*>
沒有編譯器錯誤...
確實有效。感謝您的快速回答。 – 2011-04-11 17:53:53
@ildjarn:我正在使用'&'。這足以避免複製! – Nawaz 2011-04-11 18:12:34