c ++抽象类与嵌套类。派生类和嵌套类 [英] c++ abstract class with nested class. derived class and nested class

查看:140
本文介绍了c ++抽象类与嵌套类。派生类和嵌套类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有任务要写自己的容器 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 ...

...
}


b $ b

我认为这是错的。应该嵌套类 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 Iterator 。它需要是非虚拟的,因为STL相似的迭代器应该具有值语义。有关详细信息,请参见 pimpl成语

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆