带有嵌套类的 c++ 抽象类.派生类和嵌套类 [英] c++ abstract class with nested class. derived class and nested class
问题描述
我的任务是编写自己的容器Linked_list
和Array_list
.我为他们准备了一个界面:
I have the task to write own containers Linked_list
and Array_list
. I have one interface for them:
typedef int value_type;
class Container
{
public:
class Iterator
{
public:
Iterator();
Iterator(value_type* other);
Iterator(const Iterator& other);
Iterator& operator=(const Iterator& other);
...
};
Container();
Container(const Container& other);
~Container();
virtual value_type& front() const=0;
virtual value_type& back() const=0;
virtual Iterator begin() const=0; //
...
};
我做了派生类 Linked_list 和 Array_list:
I did derived classes Linked_list and Array_list:
class Linked_list:public Container
{
public:
long int cur_size;
List elem;
static Link end_;
class Iterator: public Container::Iterator
{
friend Linked_list;
Link *p;
};
Iterator begin() const; //overriding virtual function return type differs ...
...
}
我认为这一切都错了.嵌套类 Linked_list::Iterator
应该是派生类吗?如果我不能更改界面,是否可以这样做?
I thinks it's all wrong. should nested class Linked_list::Iterator
be a derived class?
Is it possible to do this, if I can't change the interface?
推荐答案
考虑到您的设计约束不能使用模板,应该改变一件事:添加接口 IteratorImpl代码>.因此,您可以从基础
class Container
非虚拟 制作 class Iterator
.它必须是非虚拟的,因为类似 STL 的迭代器应该具有值语义.请参阅 pimpl idiom 以了解其工作原理的更多详细信息!
Taking into account your design constraints that you cannot use templates, than one thing should change: add interface IteratorImpl
. Thus you can make class Iterator
from base class Container
non virtual. It needs to be non-virtual since STL-alike iterators should have value semantics. See pimpl idiom for more details how it works!
像这样:
typedef int value_type;
class Container
{
protected:
class IteratorImpl
{
public:
virtual void next() = 0;
virtual IteratorImpl* clone() const = 0;
virtual value_type get() const = 0;
virtual bool isEqual(const IteratorImpl& other) const = 0;
};
public:
class Iterator
{
public:
Iterator(IteratorImpl* impl) : impl(impl) {}
~Iterator() { delete impl; }
Iterator(const Iterator& other) : impl(other.impl->clone()) {}
Iterator& operator=(const Iterator& other) {
IteratorImpl* oldImpl = impl;
impl = other.impl->clone();
delete oldImpl;
}
bool operator == (const Iterator& other) const
{
return impl->isEqual(*other->impl);
}
Iterator& operator ++ ()
{
impl->next();
return *this;
}
value_type& operator*() const
{
return impl->get();
}
value_type* operator->() const
{
return &impl->get();
}
};
Container();
Container(const Container& other);
~Container();
virtual value_type& front() const=0;
virtual value_type& back() const=0;
virtual Iterator begin() const=0; //
...
};
然后在你的派生中实现 IteratorImpl:
Then in your derived just implement IteratorImpl:
class Linked_list:public Container
{
protected:
class IteratorImpl: public Container::IteratorImpl
{
....
};
public:
Iterator begin() const { return new IteratorImpl(firstNode); }
Iterator end() const { return new IteratorImpl(nodeAfterLastNode); }
...
};
这些 firstNode 和 nodeAfterLastNode 只是我的猜测 - 使用您需要的任何东西来实现 IteratorImpl 接口...
These firstNode and nodeAfterLastNode are just my guess - use whatever you need to implement the IteratorImpl interface...
这篇关于带有嵌套类的 c++ 抽象类.派生类和嵌套类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!