C++ C+上的列表+;对象

C++ C+上的列表+;对象,c++,list,oop,object,C++,List,Oop,Object,您好,我想创建列表结构,但我不想使用结构,但我想使用对象。我是新手,当我写这段代码时,它就不起作用了 #include<stdlib.h> #include<iostream> #include<conio.h> #include<string.h> using namespace std; class List { public: int id; List* next = NULL; L

您好,我想创建列表结构,但我不想使用结构,但我想使用对象。我是新手,当我写这段代码时,它就不起作用了

#include<stdlib.h>
#include<iostream>
#include<conio.h>
#include<string.h>

using namespace std;

class List {
    public:
        int id;
        List* next = NULL;
        List* prev = NULL;;

        static List create(int value, int id = 0){
            List list;
            list.value = value;
            list.next = NULL;
            list.prev = NULL;
            list.id = id;
            return list;
        }

        void add(int value){
            this->next = &List::create(value, this->id + 1);
        }

        void show(){
            cout << "#" << this->id << endl << "  " << this->value << endl;
            if (this->next != NULL){
                cout << (*this->next).id;
            }
            else{
                cout << "NE ESTAS";
            }
        }

    protected:
        int value;
};

int main(void){
    cout << "Hello, It's a simple Object Lists program" << endl << endl;
    List head = List::create(1);
    head.add(2);
    head.show();
    getch();
    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
班级名单{
公众:
int-id;
List*next=NULL;
列表*prev=NULL;;
静态列表创建(int值,int id=0){
名单;
list.value=值;
list.next=NULL;
list.prev=NULL;
list.id=id;
退货清单;
}
无效添加(int值){
this->next=&List::create(值,this->id+1);
}
无效显示(){
在这里

您正在存储临时对象的地址。
this->next
是函数返回时的悬空指针

您需要使用动态内存分配对象,并将其分配给
this->next

void add(int value){
    List* ptr = new List;
    ptr->value = value;
    ptr->next = NULL;
    ptr->prev = NULL;
    ptr->id = this->id + 1;

    // Establish the pointer links between this and ptr
    List* next = this->next;

    this->next = ptr;
    ptr->prev = this;

    ptr->next = next;
    if ( next != NULL )
    {
       next->prev = ptr;
    }

}

问题在于创建函数:

   static List create(int value, int id = 0){
        List list;
        list.value = value;
        list.next = NULL;
        list.prev = NULL;
        list.id = id;
        return list;
    }
对象
列表
在堆栈上分配(作为自动变量),因此在函数返回时将被销毁。因此,在函数返回时,您的返回对象已失效且未定义

顺便说一句:类名
List
用词不当。你最好称它为
Node
,因为它只代表列表的一个节点。列表本身需要一个单独的类,这个类应该称为
List


还有一些函数是不完整的(
add
show
),但您可能知道。

下一行的问题是,您没有真正创建新项目。您只需返回一个
列表,并将其复制到head中:

List head = List::create(1);
类似地,当您将
add()
添加到head时,以下行将返回一个临时
列表
对象。在
next
中,您将获取此临时对象的地址:

this->next = &List::create(value, this->id + 1);
不幸的是,这个临时对象随后会被销毁(当您离开语句时)。因此,一旦添加完成,
next
指向一个已经无效的对象

然后调用
show()
,当您试图显示
next
的内容时,会出现一个未定义的行为(因为它指向一个不再存在的对象)

一些可能的解决方案提示:

最简单的方法是使用……但我想,为了了解这是如何工作的,你应该自己制作
列表

您的意图是以面向对象的方式工作,您应该考虑使用构造函数而不是静态创建函数:

    List (int value, int id = 0) : value(value), 
             id(id), next(nullptr), prev (nullptr) {
    }
add函数可以这样创建:

    void add(int value){
        next = new List (value, id + 1);   // dynamic allocation
        next->prev = this;  
    }
当然,这只是开始。你需要做几件事:

  • 创建一个析构函数。主要的问题是:你想只销毁一个列表,还是同时销毁它的追随者
  • 创建复制构造函数(以复制列表元素并避免浅复制)
  • 创建赋值运算符(与上面相同,因为规则为3)
    void add(int value){
        next = new List (value, id + 1);   // dynamic allocation
        next->prev = this;  
    }