【问题标题】:nonstandard iterator for subclass with containers of subclasses具有子类容器的子类的非标准迭代器
【发布时间】:2015-05-11 05:32:16
【问题描述】:

抱歉,这很长,但我无法弄清楚如何缩短它并仍然显示我的问题的重要特征。

即使我已经想出了如何做我想做的事情,我仍然在问这个问题,但它需要以与标准迭代器模式不匹配的方式实现迭代器。我想就如何以更标准或更好的方式完成这项工作征求任何建议。

我有一个抽象基类。它的任何子类都会有一些对象集合。我想将一些通用代码放入需要遍历集合中所有对象的基类中。集合中的对象都将来自另一个抽象基类,但将是子类对象。可能有来自多个子类的多个对象集合,并且这些集合可能是不同的集合类型。我真正想做的是这样的:(注意:这段代码不会编译。)

class CollectionObjectBase
{
public:

  virtual int someInterface(int x) = 0;
};

class MyObjectBase
{
public:

  class iterator
  {
  public:

    virtual CollectionObjectBase& operator*() = 0;

    virtual iterator& operator++() = 0;

    virtual bool operator!=(iterator& other) = 0;
  };

  virtual iterator begin() = 0;

  virtual iterator end() = 0;

  int processCollections()
  {
    iterator it;
    int      sum = 0;

    for(it = begin(); it != end(); ++it)
      {
        sum += (*it).someInterface(7);
      }

    return sum;
  }
};

然后我想要一些 CollectionObjectBase 和 MyObjectBase 的子类,如下所示:

class CollectionObject1 : public CollectionObjectBase { ... }
class CollectionObject2 : public CollectionObjectBase { ... }
class CollectionObject3 : public CollectionObjectBase { ... }

class MyObjectA : public MyObjectBase
{
public:

  std::map<int, CollectionObject1> someThings;
  std::vector<  CollectionObject2> otherThings;

  class iterator : public MyObjectBase::iterator
  {
  public:

    std::map<int, CollectionObject1>::iterator it1;
    std::vector<  CollectionObject2>::iterator it2;

    // Iterate first over it1 and then over it2.
    ...
  };

  virtual MyObjectBase::iterator begin()
  {
    return iterator(someThings.begin(), otherThings.begin());
  }

  virtual MyObjectBase::iterator end()
  {
    return iterator(someThings.end(), otherThings.end());
  }
};

class MyObjectB : public MyObjectBase
{
public:

  std::vector<CollectionObject1> someThings;
  std::set<   CollectionObject3> otherThings;

  class iterator : public MyObjectBase::iterator
  {
  public:

    std::vector<CollectionObject1>::iterator it1;
    std::set<   CollectionObject3>::iterator it2;

    // Iterate first over it1 and then over it2.
    ...
  };

  virtual MyObjectBase::iterator begin()
  {
    return iterator(someThings.begin(), otherThings.begin());
  }

  virtual MyObjectBase::iterator end()
  {
    return iterator(someThings.end(), otherThings.end());
  }
};

上面的代码有一些大问题。首先,MyObjectBase::begin() 和 MyObjectBase::end() 不能返回 MyObjectBase::iterator,因为 MyObjectBase::iterator 是一个抽象类,而且您无论如何都不想这样做,因为您实际上想要 MyObjectBase 的子类: :iterator 知道如何遍历 MyObjectBase 的子类中的集合。您需要返回对 MyObjectBase::iterator 的引用。

第二个问题来自 MyObjectA::iterator::operator!=()。你想做这样的事情:

virtual bool operator!=(iterator& other)
{
  return it1 != other.it1 || it2 != other.it2;
}

但是要成为 MyObjectBase::iterator::operator!=() 的重载,它必须采用 MyObjectBase::iterator& 类型的参数并且没有成员 it1 和 it2。我想解决这个问题的最好办法是用函数 atEnd() 替换 operator!=(),因为它在这段代码中的唯一用途是检测迭代器是否在末尾,但是它使它非常不标准。

最后,MyObjectA::begin() 和 MyObjectA::end() 不能返回一个临时值。请记住,我们必须返回对指向 MyObjectA::iterator 的 MyObjectBase::iterator 的引用,而不是复制构造 MyObjectBase::iterator。我想解决这个问题的最好办法是用 new 分配迭代器,当我这样做时,我需要返回一个指针,而不是一个引用,以便调用者可以删除它。所以这是我的最终代码。它工作并执行迭代器的功能,但它是非标准的。谁能想到用符合标准模式的迭代器来做到这一点?

#include <map>
#include <vector>

class CollectionObjectBase
{
public:

  virtual int someInterface(int x) = 0;
};

class MyObjectBase
{
public:

  class iterator
  {
  public:

    virtual CollectionObjectBase& operator*() = 0;

    virtual iterator& operator++() = 0;

    virtual bool atEnd() = 0;
  };

  virtual iterator* begin() = 0;

  int processCollections()
  {
    iterator* it;
    int       sum = 0;

    for (it = begin(); !it->atEnd(); ++it)
      {
        sum += (**it).someInterface(7);
      }

    delete it;

    return sum;
  }
};

class CollectionObject1 : public CollectionObjectBase
{
public:

  virtual int someInterface(int x)
  {
    return x + 1;
  }
};

class CollectionObject2 : public CollectionObjectBase
{
public:

  virtual int someInterface(int x)
  {
    return x + 2;
  }
};

class MyObjectA : public MyObjectBase
{
public:

  std::map<int, CollectionObject1> someThings;
  std::vector<  CollectionObject2> otherThings;

  class iterator : public MyObjectBase::iterator
  {
  public:

    std::map<int, CollectionObject1>::iterator it1;
    std::map<int, CollectionObject1>::iterator it1End;
    std::vector<  CollectionObject2>::iterator it2;
    std::vector<  CollectionObject2>::iterator it2End;

    iterator(std::map<int, CollectionObject1>::iterator it1Init, std::map<int, CollectionObject1>::iterator it1EndInit,
             std::vector<  CollectionObject2>::iterator it2Init, std::vector<  CollectionObject2>::iterator it2EndInit) :
      it1(it1Init),
      it1End(it1EndInit),
      it2(it2Init),
      it2End(it2EndInit)
    {
      // Initialization handled by initialization list.
    }

    virtual CollectionObjectBase& operator*()
    {
      if (it1 != it1End)
        {
          return (*it1).second;
        }
      else
        {
          return *it2;
        }
    }

    virtual iterator& operator++()
    {
      if (it1 != it1End)
        {
          ++it1;
        }
      else
        {
          ++it2;
        }

      return *this;
    }

    virtual bool atEnd()
    {
      return it1 == it1End && it2 == it2End;
    }
  };

  virtual MyObjectBase::iterator* begin()
  {
    return new iterator(someThings.begin(), someThings.end(), otherThings.begin(), otherThings.end());
  }
};

无用的答案有问题。考虑 Impl 和 Base 的这些子类:

class MyImpl : public Base::Iterator::Impl {
public:
  MyImpl(std::vector<CollectionObjectSub>::iterator itInit) : it(itInit) {}
  CollectionObjectBase& operator*() { return *it; }
  void operator++() { ++it; }
//  bool operator==(Base::Iterator::Impl const &other) const { it == other.it; }
  bool operator==(MyImpl const &other) const { it == other.it; }
private:
  std::vector<CollectionObjectSub>::iterator it;
};

class Sub : public Base {
public:
  Iterator& begin() { return Iterator(new MyImpl(collection.begin())); }
private:
  std::vector<CollectionObjectSub> collection;
};

Sub 包含一个向量,MyImpl 包含一个向量迭代器。我想用 impl_ 指向 MyImpl 实例化一个 Base::Iterator。如果 MyImpl::operator== 采用 MyImpl& 那么它不会重载 Base::Iterator::Impl::operator==,但如果它采用 Base::Iterator::Impl& 那么我无法访问向量迭代器即使它实际上是一个 MyImpl。

似乎我需要进行动态转换才能获得需要 RTTI 的 MyImpl。我听说尽可能避免使用 RTTI。还有其他方法吗?

【问题讨论】:

  • 请注意,您可以简单地断言只能比较来自同一容器的迭代器,并且由于保证它们具有相同的类型,因此静态转换应该是安全的。如果它们不是同一类型,这只是调用者的错。
  • 那行得通。在同一个对象上调用 begin() 和 end() 将始终返回相同类型的迭代器。

标签: c++ inheritance iterator


【解决方案1】:

所以,您想按值返回一个迭代器,但它的动态类型会根据您调用的begin/end 的覆盖而有所不同?没问题 - 只需添加另一层间接。

class Base {
public:
  class Iterator {
  public:
    struct Impl {
      virtual ~Impl() {}
      virtual CollectionObjectBase& operator*() = 0;
      virtual void operator++() = 0;
      virtual bool operator==(Iterator::Impl const &other) const = 0;
    };
    Iterator() {}
    explicit Iterator(std::unique_ptr<Impl> &&i) : impl_(std::move(i)) {}
    Iterator(Iterator &&other) = default;

    CollectionObjectBase& operator*() { return **impl_; }
    Iterator& operator++() { ++*impl_; return *this; }
    bool operator==(Iterator const &other) const {
      return (!impl_ && !other.impl_) || (*impl_ == *other.impl_);
    }
  private:
    std::unique_ptr<Impl> impl_;
  };
  // ...

这使用 pointer-to-impl (pimpl) 习惯用法为Base 的每个具体子类赋予其自己的Base::Iterator::Impl 的具体子类,但仍然允许您传递Base::Iterator 对象按值使用移动语义来转移动态对象的所有权。

注意。我让两个默认构造的 (nullptr) 迭代器比较相等,因此您无需为 end 创建一个真实实例。

【讨论】:

  • 其实这个还是有问题的。当我去实现 Impl 的子类时,operator== 有问题。要演示的代码对于此评论来说太长了,所以我将其添加到问题中。
猜你喜欢
  • 1970-01-01
  • 2018-02-06
  • 1970-01-01
  • 2018-05-16
  • 2012-07-14
  • 2018-12-31
  • 2011-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多