访问祖父母虚拟方法

Access grandparent virtual method

本文关键字:方法 虚拟 祖父母 访问      更新时间:2023-10-16

我有三个类:

template<typename E>
class Iterator {
public:
    virtual ~Iterator() {
    }
    virtual bool hasNext() const = 0;
    virtual const E& next() = 0;
};
template<typename E>
class IteratorPtr {
private:
    Iterator<E>* iterator;
    IteratorPtr(const IteratorPtr<E>&);
    IteratorPtr<E>& operator=(const Iterator<E>&);
public:
    IteratorPtr(Iterator<E>* it)
            : iterator(it) {
    }
    ~IteratorPtr() {
        delete iterator;
    }
    Iterator<E>* operator->() const {
        return iterator;
    }
};    
template<typename E>
class Collection
{
public:
    virtual void add(const E & value) = 0;
    virtual void add(const Collection<E>& collection);
    virtual bool remove(const E& value) = 0;
    virtual void clear() = 0;
    virtual ~Collection()
    {
    }
    virtual bool isEmpty() const = 0;
    virtual int size() const = 0;
    virtual bool contains(const E& value) const = 0;
    virtual Iterator<E>* iterator() const = 0;
};
void Collection<E>::add(const Collection<E>& collection)
{
for (IteratorPtr<E> i(collection.iterator()); i->hasNext();) {
    this->add(i->next());
}
}

对象:所有Set的方法都被实现。

template<typename E>
class Set;
template<typename E>
class SortedSet;
template<typename E>
class SetIterator;
template<typename E>
class SetNode {
private:
    friend class Set<E> ;
    friend class SortedSet<E> ;
    friend class SetIterator<E> ;
    SetNode()
            : next(NULL) {
    }
    SetNode(E value)
            : value(value), next(NULL) {
    }
    SetNode(E value, SetNode * next)
            : value(value), next(next) {
    }
    ~SetNode() {
    }
private:
    E value;
    SetNode * next;
};
template<typename E>
class SetIterator: public Iterator<E> {
private:
    friend class Set<E> ;
    SetIterator(SetNode<E> * head)
            : node(head) {
    }
    ~SetIterator() {
    }
    bool hasNext() const {
        return node != NULL;
    }
    const E & next() {
        E& value = node->value;
        node = node->next;
        return value;
    }
private:
    SetNode<E> * node;
};
template<typename E>
class Set: public Collection<E> {
public:
    Set(): numNodes(0), head(NULL) {
    }
    virtual ~Set() {
        clear();
    }
    virtual void add(const E & value);
    virtual bool remove(const E& value);
    virtual void clear();
    virtual bool isEmpty() const;
    virtual int size() const;
    virtual bool contains(const E& value) const;
    virtual Iterator<E>* iterator() const;
private:
    Set(const Set & obj): numNodes(0), head(NULL) {
    }
    virtual bool contains(const E & value, SetNode<E> * & previ) const;
protected:
    int numNodes;
    SetNode<E> * head;
};
template<typename E>
class SortedSet: public Set<E> {
private:
    virtual bool contains(const E& value, SetNode<E> *& prev) const;
public:
    SortedSet(): Set<E>() {
    }
    virtual void add(const E & value);
    virtual ~SortedSet() {
        this->clear();
    }
};

Set和SortedSet不实现add方法,因为它们应该做的正是Collection::add(const Collection&c)。

int main() {
Set<int> *s = new Set<int>();
s->add(10);
s->add(30);
s->add(12);
s->remove(10);
if (s->contains(30))
    puts("Tem");
SortedSet<int> *ss = new SortedSet<int>();
ss->add(*s);
return 0; 
}

但这段代码在"ss->add(*s);"行中出现错误,说:与' SortedSet::add(Set&) '不匹配

为什么会发生这种情况?

现在您已经发布了所有相关代码,问题是您已经在Set中声明了另一个同名函数:

virtual void add(const E & value);

隐藏基类中同名的所有内容;所以Collection::add不能通过引用Set或它的任何子类来访问。

要解决这个问题,向Set添加一个using声明,将Collection::add纳入Set的作用域:

public:
    using Collection::add;