C++、多态和迭代器 [英] C++, polymorphism and iterators

查看:69
本文介绍了C++、多态和迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个存储接口(抽象类)和一组存储实现(SQLite、MySQL、Memcached ..),用于存储已知类的对象并从存储中检索子集.
对我来说,清晰的界面是:

I want to have a Storage interface(abstract class) and a set of Storage implementations (SQLite, MySQL, Memcached..) for storing objects of a known class and retrieving subsets from the Storage.
To me the clear interface would be:

class Storable{int id; blah; blah; blah; string type;};
class Storage{
    virtual Storage::iterator get_subset_of_type(string type) = 0;
    virtual Storage::iterator end)_ = 0;
    virtual void add_storable(Storable storable) = 0;
};

然后创建实现接口的 Storage 实现.现在,我的问题如下:

And then create implementations of Storage that fulfill the interface. Now, my problem is the following:

  • 迭代器不能是多态的,因为它们是按值返回的.
  • 我不能只为给定的 Storage 实现子类化 Storage::iterator
  • 我曾想过使用一个包装迭代器来包装并在 Storage 实现子类化的多态类型上执行 pimpl,但随后我需要使用动态内存并在所有地方进行分配.

有什么提示吗?

推荐答案

如果你想要一个用于迭代的虚拟接口,像这样?

If you want a virtual interface for iteration, something like this?

#include <iostream>
#include <iterator>

struct Iterable {
    virtual int current() = 0;
    virtual void advance() = 0;
  protected:
    ~Iterable() {}
};

struct Iterator : std::iterator<std::input_iterator_tag,int> {
    struct Proxy {
        int value;
        Proxy(const Iterator &it) : value(*it) {}
        int operator*() { return value; }
    };
    Iterable *container;
    Iterator(Iterable *a) : container(a) {}
    int operator*() const { return container->current(); }
    Iterator &operator++() { container->advance(); return *this; }
    Proxy operator++(int) { Proxy cp(*this); ++*this; return cp; }
};

struct AbstractStorage : private Iterable {
    Iterator iterate() {
        return Iterator(this);
    }
    // presumably other virtual member functions...
    virtual ~AbstractStorage() {}
};

struct ConcreteStorage : AbstractStorage {
    int i;
    ConcreteStorage() : i(0) {}
    virtual int current() { return i; }
    virtual void advance() { i += 10; }
};

int main() {
    ConcreteStorage c;
    Iterator x = c.iterate();
    for (int i = 0; i < 10; ++i) {
        std::cout << *x++ << "\n";
    }
}

这不是一个完整的解决方案 - 我还没有实现 Iterator::operator==Iterator::operator->(后者是必需的如果包含的类型是类类型).

This isn't a complete solution - I haven't implemented Iterator::operator==, or Iterator::operator-> (the latter is needed if the contained type is a class type).

我在 ConcreteStorage 类中存储状态,这意味着我们不能同时在同一个 Storage 上有多个迭代器.所以可能不是 Iterable 是 Storage 的基类,需要有另一个 Storage 的虚函数来返回一个新的 Iterable.它只是一个输入迭代器这一事实意味着迭代器的副本都可以指向相同的 Iterable,因此可以使用 shared_ptr(以及 Itertable 应该有一个虚析构函数,或者 newIterator 函数应该返回 shared_ptr,或者两者都返回).

I'm storing state in the ConcreteStorage class, which means we can't have multiple iterators on the same Storage at the same time. So probably rather than Iterable being a base class of Storage, there needs to be another virtual function of Storage to return a new Iterable. The fact that it's only an input iterator means that copies of an iterator can all point to the same Iterable, so that can be managed with a shared_ptr (and either Itertable should have a virtual destructor, or the newIterator function should return the shared_ptr, or both).

这篇关于C++、多态和迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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