gpt4 book ai didi

c++ - 访问祖 parent 虚方法

转载 作者:太空宇宙 更新时间:2023-11-04 11:58:05 25 4
gpt4 key购买 nike

好吧,我有三个类:

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;

关于c++ - 访问祖 parent 虚方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15286881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com