不同类的链表,如何用指针访问特定的类函数?

时间:2020-12-07 14:29:14

Ok so I have a linked list of Personnel. However, I can add to the list a Personnel, an Employee, a Faculty, or a Student, because the other 3 are child classes.

好的,我有一个人员链表。但是,我可以在列表中添加人员,员工,教师或学生,因为其他3个是子类。

Teacher directions: "Add a sub menu item and related functions (insert book) to allow a new book to be added into the book tree of an existing student node. The new book must be inserted into the correct place to maintain the binary search tree feature. The information of student name, book title, and url must be entered. To simply the program, you can assume that all the nodes in the linked list are Student nodes."

教师指导:“添加子菜单项和相关功能(插入书籍),以允许将新书添加到现有学生节点的书籍树中。必须将新书插入到正确的位置以维护二叉搜索树必须输入学生姓名,书名和网址的信息。对于简单的程序,您可以假设链接列表中的所有节点都是学生节点。“

^^ ** I want to point out that I see where she says to simplify assume all the linked lists are student nodes, but all of the code for Personnel/Employee/Faculty and the linked list is supplied in the assignment. So, the only way to "use" that I can think of is go through all the code any change any reference from Personnel to Student, but that seems like a fairly stupid assignment at that point...

^^ **我想指出我看到她说要简化的地方假设所有链接列表都是学生节点,但Personnel / Employee / Faculty的所有代码和链接列表都在赋值中提供。所以,我能想到的“使用”的唯一方法就是通过所有代码来改变从人员到学生的任何参考,但这似乎是一个相当愚蠢的任务......

So my problem: I need to go through the linked list until I find the right entry using only name field. But then I need to edit the BookTree class that is tied to that entry, which only the child class Student has. I can't loop through the linked list with a Student pointer (although, the list is empty of anything other than students) because of type mismatch w/ Personnel, but if I find it by using a Personnel pointer I, of course, won't have access to Student specific functions to edit what I need to.

所以我的问题:我需要浏览链表,直到我找到只使用名称字段的正确条目。但是我需要编辑与该条目绑定的BookTree类,该类只有Student类具有的子类。我不能用学生指针遍历链表(虽然,列表中没有学生以外的任何东西),因为类型不匹配w / Personnel,但如果我通过使用Personnel指针找到它,当然,赢了无法访问学生特定功能来编辑我需要的内容。

I have tried Student *ptr = &PersonnelPtr to just assign it the address after I located the correct one, no dice.

我已经尝试过Student * ptr =&PersonnelPtr,只需在找到正确的地址后给它分配地址,没有骰子。

The code:

int add_book()
{
    char studentName[50]; // blah
    PersonnelNode *temp, *prev;
    Student *student;       
    temp = head;
    prev = temp;
    while (temp != NULL)
    {
        student = temp->getNode(); // this line gives "value of type Personnel* cannot be assigned to an entity of type Student*
        if (_stricmp(studentName, student->getName()) != 0) 
        {
            prev = temp; // loop through
            temp = temp->getNext();
            index++;
        }
        else
        {
            // Do Stuff
            // More importantly in the do stuff part I need access to Student-specific data/functions that Personnel pointer won't see

1 个解决方案

#1


You should use a pointer to Personnel to control the other entities (children). Right now you're doing the other way around, and the compiler tells you that the right hand side (a Personnel*) cannot be converted to a Student*. That's because down-casting is not possible implicitly. In general, a Derived is a Base, so you can convert implicitly from Derived* to Base* (upcasting), but not the other way.

您应该使用指向Personnel的指针来控制其他实体(子)。现在你正在做相反的事情,编译器告诉你右手边(Personnel *)无法转换为Student *。那是因为无法隐式地进行下击。通常,Derived是Base,因此您可以隐式地从Derived *转换为Base *(向上转换),但不能以其他方式转换。

See e.g. this tutorial for some more information about up/down casting.

参见例如有关上/下投射的更多信息,请参阅本教程。

EDIT If you know your Personel* pointer points to a Student, then you can use dynamic_cast<Student*>(your_pointer) to access the Student features of your object. However, casts are in general a sign of imperfect design, and you should try to avoid them. In particular, how would you know what are the dynamic types of your objects in the list (assuming you get the list already filled by some other function)? You can use RTTI (testing typeid results then dynamic_cast-ing), but it is more than painful and not really recommended. It is much better to implement a common virtual interface and let the compiler deal with choosing the right virtual function at runtime.

编辑如果您知道Personel *指针指向学生,则可以使用dynamic_cast (your_pointer)来访问对象的学生功能。但是,演员阵容通常是不完美设计的标志,你应该尽量避免使用它们。特别是,您如何知道列表中对象的动态类型(假设您已经通过其他函数填充了列表)?您可以使用RTTI(测试typeid结果然后使用dynamic_cast-ing),但这不仅仅是痛苦而且不是真正推荐的。实现一个通用虚拟接口并让编译器处理在运行时选择正确的虚函数要好得多。

#1


You should use a pointer to Personnel to control the other entities (children). Right now you're doing the other way around, and the compiler tells you that the right hand side (a Personnel*) cannot be converted to a Student*. That's because down-casting is not possible implicitly. In general, a Derived is a Base, so you can convert implicitly from Derived* to Base* (upcasting), but not the other way.

您应该使用指向Personnel的指针来控制其他实体(子)。现在你正在做相反的事情,编译器告诉你右手边(Personnel *)无法转换为Student *。那是因为无法隐式地进行下击。通常,Derived是Base,因此您可以隐式地从Derived *转换为Base *(向上转换),但不能以其他方式转换。

See e.g. this tutorial for some more information about up/down casting.

参见例如有关上/下投射的更多信息,请参阅本教程。

EDIT If you know your Personel* pointer points to a Student, then you can use dynamic_cast<Student*>(your_pointer) to access the Student features of your object. However, casts are in general a sign of imperfect design, and you should try to avoid them. In particular, how would you know what are the dynamic types of your objects in the list (assuming you get the list already filled by some other function)? You can use RTTI (testing typeid results then dynamic_cast-ing), but it is more than painful and not really recommended. It is much better to implement a common virtual interface and let the compiler deal with choosing the right virtual function at runtime.

编辑如果您知道Personel *指针指向学生,则可以使用dynamic_cast (your_pointer)来访问对象的学生功能。但是,演员阵容通常是不完美设计的标志,你应该尽量避免使用它们。特别是,您如何知道列表中对象的动态类型(假设您已经通过其他函数填充了列表)?您可以使用RTTI(测试typeid结果然后使用dynamic_cast-ing),但这不仅仅是痛苦而且不是真正推荐的。实现一个通用虚拟接口并让编译器处理在运行时选择正确的虚函数要好得多。