C++ c++;带链表的虚拟函数

C++ c++;带链表的虚拟函数,c++,function,virtual,C++,Function,Virtual,我在尝试在类中使用虚拟函数时遇到了一些问题。 我使用一个链表来存储Employee、Staff和Managers,它们相互继承(Staff和Managers继承Employee基类) 我需要能够访问一个名为getType的函数,该函数根据所属的类返回“staffee”或“Manager” 这段代码是我创造的员工和经理 Staff staff4 = { "Lisa", "22/02/2012", 0004, HR, 8.9, 34.50 }; Employee* pStaff4 =

我在尝试在类中使用虚拟函数时遇到了一些问题。 我使用一个链表来存储Employee、Staff和Managers,它们相互继承(Staff和Managers继承Employee基类)

我需要能够访问一个名为getType的函数,该函数根据所属的类返回“staffee”或“Manager”

这段代码是我创造的员工和经理

    Staff staff4 = { "Lisa", "22/02/2012", 0004, HR, 8.9, 34.50 };
    Employee* pStaff4 = &staff4;
    Employee& testStaff4 = staff4;
    myList->addInFront(testStaff4);

    Staff staff5 = { "Jade", "23/03/2014", 0003, HR, 6.4, 38.50 };
    Employee* pStaff5 = &staff5;
    Employee& testStaff5 = staff5;
    myList->addInFront(testStaff5);

    Manager manager1 = { "Lily", "01/09/2012", 0001, MARKETING, 75968 };
    Employee* pMan1 = &manager1;
    Employee& testMan1 = manager1;
    myList->addInFront(testMan1);

    Manager manager2 = { "Craig", "27/03/2011", 0002, HR, 82478 };
    Employee* pMan2 = &manager2;
    Employee& testMan2 = manager2;
    myList->addInFront(testMan2);

    //cout << pStaff5->getType();
    //system("pause");
最后,我尝试调用getType()函数:

void displayList(const List& list)
{
    List temp(list);
    while (!temp.isEmpty())
    {

        cout << temp.first()->item.getType() << "\n";
        cout << temp.first()->item.getName() << "\n";
        temp.deleteFirst();
    }

}
无效显示列表(常量列表和列表)
{
列表温度(列表);
而(!temp.isEmpty())
{
cout item.getType()项=数据;
nodePtr->next=头部;
head=nodePtr;
}
节点*列表::搜索(const long longID)
{
}
bool List::searchDelete(const long longID)
{
节点*temp,*prevNode;
温度=水头;
prevNode=NULL;
while(temp!=NULL)
{
}
}
节点*列表::end()常量
{
if(head==nullptr)
返回空ptr;
其他的
{
节点*nodePtr=头部;
while(nodePtr->next!=nullptr)
{
nodePtr=nodePtr->next;
}
返回nodePtr;
}
}
无效列表::添加结束(常量项和数据)
{
Node*nodePtr=新节点;
断言(nodePtr!=nullptr);
if(head==nullptr)
{
head=nodePtr;
nodePtr->item=数据;
}
其他的
{
nodePtr->item=数据;
节点*ptr=end();
ptr->next=nodePtr;
}
}
列表和列表::运算符=(常量列表和rhs)
{
如果(&rhs!=此)
{
破坏();
副本(rhs);
}
归还*这个;
}
无效列表::副本(常量列表和其他列表)
{
}
作废列表::销毁()
{
while(head!=nullptr)
{
节点*ptr=头部;
头部=头部->下一步;
删除ptr;
}
}
列表::~List()
{
}
对这些文件的长度表示歉意

我不明白为什么它不会调用适当的虚函数,正如您在我使用pStaff5->getType()的第一个代码片段中看到的那样,它起了作用-但是一旦我将节点存储在链接列表中,我就无法访问这样的节点…(可以吗?)

问候
Craig

您的列表节点存储一个
项,但这只是基类。当您尝试将
管理器
职员
放入列表时,仅将对象的基类部分复制到列表中(称为),而不是对象的派生部分

当您调用virtual函数时,您只会得到virtual的基类重写器,因为列表中存储的对象只是
员工


(应考虑将<代码>节点< /代码>和<代码>列表>代码>模板,而不是执行<代码>定义项目项

您需要显示“代码>列表< /代码> -它可能无法存储子代码类<代码>雇员< /代码>。Yea,您发送给DISPLISTLISH的“列表”的定义是什么?.我敢打赌这是列表;
first()是什么意思->item
return?它应该是引用或指针类型,多态性才能正常工作。@AtlasC1它返回一个名为Node的结构,其中包含一个项和指向下一个节点的指针。如果您的链表节点存储Employee类型的对象,则存储从Employee继承的任何对象都将导致对象切片。这是您的问题您需要在链接列表节点中存储员工指针。
void displayList(const List& list)
{
    List temp(list);
    while (!temp.isEmpty())
    {

        cout << temp.first()->item.getType() << "\n";
        cout << temp.first()->item.getName() << "\n";
        temp.deleteFirst();
    }

}
//#include <string>
#include "Employees.h"

#define Item Employee

using namespace std;

struct Node
{
    Item item;
    Node* next;
};

class List
{
private: 
    Node* head;
    Node* end() const;
    void copy(const List&);
    void destroy();
public:
    List();
    List(const List&);
    ~List();
    List& operator=(const List&);
    bool operator==(const List&);
    bool isEmpty() const;
    Node* first() ;
    Item last() const;
    List tail() const;
    void addInFront(const Item&);
    void addAtEnd(const Item&);
    void deleteFirst();
    Node* search(const long);
    bool searchDelete(const long);
};
#include "stdafx.h"
#include "List.h"
#include <assert.h> 

List::List()
{
    head = NULL;
}

List::List(const List& otherList) : head(nullptr)
{
    copy(otherList);
}

bool List::isEmpty() const
{
    return (head == nullptr);
}

Node* List::first() 
{
    assert(head != nullptr);
    return head;
}

void List::deleteFirst()
{
    if (head != NULL) 
    {
        Node* tmp = head->next;
        delete head;
        head = tmp;
    }
}

void List::addInFront(const Item& data)
{
    Node* nodePtr = new Node;
    assert(nodePtr != nullptr);
    nodePtr -> item = data;
    nodePtr ->next = head;
    head = nodePtr;
}

Node* List::search(const long longID)
{

}


bool List::searchDelete(const long longID)
{

    Node *temp, *prevNode;
    temp = head;
    prevNode = NULL;
    while (temp != NULL)
    {

    }
}

Node* List::end() const
{
    if (head == nullptr)
        return nullptr;
    else
    {
        Node* nodePtr = head;
        while (nodePtr->next != nullptr)
        {
            nodePtr = nodePtr->next;
        }
        return nodePtr;
    }
}

void List::addAtEnd(const Item& data)
{
    Node* nodePtr = new Node;
    assert(nodePtr != nullptr);
        if (head == nullptr)
        {
            head = nodePtr;
            nodePtr->item = data;
        }
        else
        {
            nodePtr->item = data;
            Node* ptr = end();
            ptr->next = nodePtr;
        }
}

List& List::operator=(const List& rhs)
{
    if (&rhs != this)
    {
        destroy();
        copy(rhs);
    }
    return *this;
}

void List::copy(const List& otherList)
{

}

void List::destroy()
{
    while (head != nullptr)
    {
        Node* ptr = head;
        head = head->next;
        delete ptr;
    }
}

List::~List()
{

}