1

我制作了一个通用的抽象链表(AbstractList),它包含 Data*,并具有内部类 Iterator。然后,我为 Job 类对象创建了一个从 AbstractList 派生的链表,称为 JobList。

问题是,我无法在 AbstractList 的 Iterator 类中实现 operator*,因为它必须返回抽象的 Data。

所以,我认为我必须在派生类 JobList 中实现它,但我不能。这是代码,我哪里出错了?

class AbstractList
{
protected:
....//Node code
....

Node *m_head;
int m_len;

public:
    class Iterator
    {
    private:
        Node *curr;
    public:
        Iterator(Node* nd): curr(nd){}
        Iterator(const Iterator& it) {curr=it.curr;}
        Iterator operator++() {curr=curr->getNext(); return *this;}
        bool operator==(const Iterator& other) {return curr==other.curr;}
        bool operator!=(const Iterator& other) {return curr!=other.curr;}
       //here I should implement operator *, but I can't, so I do it in JobList
    };
    Iterator begin() {return Iterator(m_head);}

    AbstractList();
    ~AbstractList();

    virtual void setNewNode(Data*)=0;
    Data *getFirst() const {return m_head->getData();}
};

和派生类 JobList:

class JobList: public AbstractList
{
public:
    void setNewNode(Job*);
    Job *getFirst() const {return (Job*)AbstractList::GetFirst();}

    //here I want to implement operator*, but everything I've tried failed.
    //How should it be done?
};

编辑:我试图将以下代码放在 AbstractList 的迭代器中:

Data *operator*() {return curr->getData();}

而派生类JobList中的这段代码:

Job *Iterator::operator*() {return (Job*)Iterator::operator*}

但是我得到以下错误:'JobList':类包含显式覆盖'*',但不是从包含函数声明的接口派生的。

4

0 回答 0