Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Linkedlist在cpp中包含不同的对象类_C++_Oop - Fatal编程技术网

C++ Linkedlist在cpp中包含不同的对象类

C++ Linkedlist在cpp中包含不同的对象类,c++,oop,C++,Oop,如何创建包含不同对象类的链表。例如,如果一个人是基类,它将student和teacher作为派生类,那么我如何创建一个链表,该链表可以同时包含student和teacher这两类对象 LinkedList example for understanding my problem: head->studentobject->teacherobject->studentobject->teacherobject... so on 试试看。std::list是一个链表 #i

如何创建包含不同对象类的链表。例如,如果一个人是基类,它将student和teacher作为派生类,那么我如何创建一个链表,该链表可以同时包含student和teacher这两类对象

LinkedList example for understanding my problem:
head->studentobject->teacherobject->studentobject->teacherobject... so on

试试看。
std::list
是一个链表

#include <list>
#include <iostream>
#include <memory>
using namespace std;

class Person {
public:
    virtual ~Person() = default;
    virtual void talk()=0;
};

class Student : public Person {
public:
    void talk() {
        cout << "Teach me!\n";
    }
};

class Teacher : public Person {
    public:
    void talk() {
        cout << "Listen!\n";
    }
};

int main() {
    list<std::unique_ptr<Person>> people;
    people.push_back(unique_ptr<Person>(new Student()));
    people.push_back(unique_ptr<Person>(new Teacher()));

    for (auto& p : people) {
        p->talk();
    }

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
班主任{
公众:
virtual~Person()=默认值;
虚拟空谈()=0;
};
班级学生:公众人士{
公众:
空谈{

cout通常您会创建基类和派生类:

#include <iostream>
#include <list>
#include <memory>

class Base {
public:
    virtual ~Base() = default;

    virtual void printClass() = 0;
};

class Student : public Base {
public:
    virtual void printClass() { std::cout << "Student\n"; }
};

class Teacher : public Base {
public:
    virtual void printClass() { std::cout << "Teacher\n"; }
};

class Node {
public:
    void setTeacher() {
        data = std::make_unique<Teacher>();
    }
    void setStudent() {
        data = std::make_unique<Student>();
    }

    void printClass() { data->printClass(); }

private:
    std::unique_ptr<Base> data;
};

int main() {
    std::list<Node> l;
    l.push_back({});
    l.front().setTeacher();
    l.front().printClass();
}
#包括
#包括
#包括
阶级基础{
公众:
virtual~Base()=默认值;
虚空printClass()=0;
};
班级学生:公共基地{
公众:

virtual void printClass(){std::cout列表节点中的数据应该是指向基类对象的指针。您在这个问题上有“C”标记,我删除了这个标记,因为这个问题显然与C无关,C没有“基类”和“派生类”的概念(请注意,C与C++是不同的语言).嘿,非常感谢你的回答。有没有其他方法,因为我是从头开始实现链表的,所以我如何声明包含引用派生对象和下一个链接的成员的结构?@sahilmakandar不要将所有功能捆绑到一个概念中。试着模仿你自己的st实现d::使用模板列出,然后使用我的答案中的逻辑。类似这样的东西,也许你可以将节点作为模板……我的意思是,我不知道他们为什么在2019年在该链接中教授使用原始指针,但你明白了要点。嘿,非常感谢你的回答。还有其他方法吗,因为我是从那个么我如何才能声明这个包含引用派生对象和下一个链接的成员的结构呢?