C++ 类的空间分配问题

请问为什么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;
};

list是一个链表,链表中通过指针访问,链表中的每个节点都是通过指针指向下一个节点,如果不用new申请空间,那么得到的节点都是局部变量,会随着作用域的结束被系统回收,空间中的数据也就会被丢弃,但是用new以后,只要不主动delete(或者退出程序),这些空间时不会回收的,这些空间里的数据也就不会被随意修改。
所以,在list中,new listnode就能够得到一个节点的空间,这个空间不会被其它程序或系统随意修改,data作为在listnode中的数据也就不会被修改,data本身在listnode的内存空间中,所以就不需要再额外申请空间。当然,如果data是一个指针类型的数据,那么也需要在使用前用new申请空间,因为,listnode中存储的是data的值,如果data是一个指针,那么data的大小只有一个指针的大小,所以,如果data是指针类型,也是需要在使用前重新申请内存。