请问为什么list的构造函数中,需要用new分配空间?
定义一个新的类的时候,是所有包含的数据都需要用new分配空间吗?但listnode中data似乎就没有用new分配空间
非常感谢!
template<class type> class list;
template<class type> class listnode {
friend class list<type>; //注意这里写的是list<type>,没有template,还需要加<type>
public:
listnode() { next = NULL; }
listnode(const type& item) { data = item; } // 利用data创建新的变量
listnode* nextnode() { return next; } //下一节点的地址
void insertafter(listnode<type>*); //在当前节点后续插入
void removeafter();
private:
type data;
listnode<type>* next; //注意这里需要加<type>
};
template<class type>class list {
public:
list(const type& value) { head = tail = new listnode<type>(value); }
~list();
listnode<type>* createlist(int n);
void makeempty();
int length() const;
listnode<type>* find(type value);
int insert(type value, int n);
type* delete(int i);
type* get(int i);
private:
listnode<type>* head, * tail;
};