我制作了一个包含Data *的通用抽象链表(AbstractList),并且具有内部类Iterator。然后,我创建了一个链接列表,该列表源自AbstractList,用于Job类对象,称为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的Iterator中:
Data *operator*() {return curr->getData();}
这个代码在派生类JobList:
中Job *Iterator::operator*() {return (Job*)Iterator::operator*}
但我得到以下的erorr: 'JobList':类包含显式覆盖'*',但不是从包含函数声明的接口派生的。