c++ abstract base class postfix operator

两盒软妹~` 提交于 2019-12-01 21:16:59

问题


I have a question about implementing a shared iterator interface.

As common practice for postix operator the function might look like this:

IteratorClass operator ++(int) {
  IteratorClass temp = *this;

  //increment stuff

  return temp
}

And most of the time this is fine. In my case I am trying to implement 3 iterators for one class. Each iterator will load a local collection class with data but each derived iterator would load it in a different way. Because the collection class would be the same and all of the code for the operators( postfix/prefix ++/--, *) would be the same I thought a nice way to implement this would be inheritance:

struct iterator {
  protected:
  Collection collection;
  public:
  operator++(int);
  operator++;
  operator--(int);
  operator--;

  virtual load() = 0;

}

struct iterator1 : public iterator {
  virtual load() { custom load function }
}

struct iterator2 : public iterator {
  virtual load() { custom load function }
}

The problem are the postfix operators... They are trying to create an object of an abstract type and then returning it. Any suggestions for workarounds or structure changes?


回答1:


Use the CRTP idiom to make the base class aware of the final class. For example:

template<typename T>
struct iterator_base {
  public:
  T operator++(int) {
    T temp = static_cast<T&>(*this);
    ++*this;
    return temp;
  }
  T& operator++() {
    // ++ mutation goes here
    return *this;
  }
  // ... likewise for --, etc.
};

struct iterator1: public iterator_base<iterator1> {
  // ... custom load function
};

This approach is called static polymorphism, and allows you (in some cases) to completely eschew virtual and therefore make your objects smaller. You can omit the load declaration from the base classes and call T::load as static_cast<T&>(*this).load().



来源:https://stackoverflow.com/questions/18172858/c-abstract-base-class-postfix-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!