Suppose I have an abstract container-like class called RuleBook
. Users of RuleBook
expect to be able to forward-iterate over the RuleBook
to obtain Rule
s.
Unlike standard containers, there will be no restrictions on the memory layout of the the concrete subclasses here. Instead, it is up to the implementers of the subclasses to comply with the forward-iteration requirement of RuleBook
and satisfy this requirement based on its own data structures.
I am thinking that RuleBook
should contain pure virtual begin()
and end()
so it would work with range-based for, but I am running into a few problems.
What should the signatures be for begin() and end()? How should BasketballRules and CompanyRules be implemented?
How do we deal with the end condition when the iterator goes past the last item?
In the example below, you can assume that m_rp
and m_rpp
only point to one Rule each. We want to return some kind of iterator for the guts (like a Rule*
). We can also assume that all subclasses of Foo will contain Rule
in various data structures, which will be up to the whim of the implementor.
If I implement the entire thing using Rule*
as my iterator and null_ptr
as my beyond-the-endpoint, would this be STL compliant?
I am currently looking into custom iterators, but I'm not even sure this problem fits well with that paradigm because each subclass must effectively define the guts of the iteration.
CODE
struct RuleBook
{
// virtual Rule* begin() = 0;
// virtual Rule* end() = 0;
};
struct CompanyRules :
public RuleBook
{
Rule m_r;
};
struct BasketballRules :
public RuleBook
{
// return the three Rules, one-at-a-time, in succession
Rule m_r;
Rule* m_rp;
Rule** m_rpp;
};
int
main( int argv, char* argc[] )
{
}