我有一个通用的清单与模板通用列表中删除非指针
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