English 中文(简体)
C++, 多种形态和异构体
原标题:C++, polymorphism and iterators

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;
};

然后,建立符合接口的储存。 现在,我的问题是:

  • Iterators can t be polymorphic as they are returned by value.
  • I can t just subclass Storage::iterator for my given Storage implementation
  • I thought about having a wrapper iterator that wraps and does pimpl over a polymorphic type that the Storage implementations subclass, but then I need to use dynamic memory and allocate all over the place.

Any hint?

最佳回答

如果你们想要一个虚拟的交接点,那就象这样?

#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++ << "
";
    }
}

这一完整的解决办法——In t 已执行Iterator:operator=Iterator:operator->(如果所含类型为一类,则需要后者)。

混凝土类中的I m 储存状态,这意味着我们同时可以在同一个储存上拥有多个储存器。 也许不能将<代码>易读作为储存的基类,因此,储存还需要另一种虚拟功能,以退回新的<代码>。 可检索。 只提供一种投入,这意味着,发送器的复印件都可以注明相同的<条码>。 (和Itertable 应具有虚拟脱轨器,或新的电离机功能应退还commd_ptr,或两者兼有)。

问题回答

我看不到储存的好处。

无论如何,都指出,变压器必须完全做到多变。

它只是为了其功能而使用储存舱的虚拟方法。 那么,这些方法在后代中很容易被压倒一切(激发预期功能)。

我不确切地说,为什么这是一个问题。 您只是需要执行所有经营者(增减、减优惠等),以便他们采用“<代码”的虚拟方法。 存储目标。

你重新利用数据库发动机进行储存,这一事实并没有改变以下事实:你在这里的基本上是一个集装箱类别。

简言之,你几乎肯定应该使用一个在所储存的物体类型上瞬时出现的分类模板。 储存发动机的变数可以通过继承或通过第二个模板论点处理。 采用模板论点,可以汇编时间多变,而继承则带来时间长的多变(即,你可以在时间上改变储存发动机)。

您不妨查看rel=“nofollow”>DTL,以了解一些启发(或您可以避免许多麻烦,并且只是加以利用,直到或除非你遇到问题)。

这似乎是一个动态分配与改变国家规模的能力(不与客户重合)的情况。

您可以尝试增强力:提供导师、若干适应者和校友。

检查对象:任何从业者或其他某些从业者(http://thbecker.net/free_software_utilities/type_erasure_for_cpp_iterators/any_iterator.html)。 它采用多变异构体概念,但你仍然以价值(按价值回归、按价值转折等)处理任何变质。

Your algorythms code that actualy uses different types of containers has to be written in template functions (possibly member functions), who get the types through the template s parameters. That resolves at compile time. And the exact iterator type is known for each instanciation of that template function.

If you really need a runtime resolution, you need to add a dispatch to call different instanciations of the above template function. A virtual function should call the actual algorythm template function, but that would be a virtual function override in some template class (i.e. the virtual function will be compiled separately for each instanciation and call a different instanciation of the algorythm template function). If you need a double/multiple dispatch, so be it. Too bad that c++ doesn t support function to be virtual on multiple parameters, you ll have to use any of the common idioms for double dispatch. But the call for the actual algorythm function should be after the dispatch is resolved.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签