c++ - Iterator for custom container with derived classes -
I have a custom container that has been implemented in two different ways, but with one interface. Something like this
square vector {virtual Iterator start () = 0; Virtual Iterator and () = 0; ... // some other functions}; Square vector impa: public vector {start of theater () {return m_data.begin (); } Iterator and () {return m_data.end (); } Private: SomeFloatContainer m_data; }; Square vector IMPL: public vector {starters (starters) {return m_data.end (); } Iterator and (); {Return m_data.end (); } Private: std :: vector & lt; Float & gt; M_data; };
I need an integrated interface for the Iterator, so that I can use it in the base class. Any ideas?
I have already run this problem on my own. There are several ways to solve your problem, but you should leave the idea of Venture Base Class. Copy about the way C + STL container is designed.
STL contains concepts instead of base class std :: vector
is a model of a container
concept, but The container is not derived from the base class. A concept is a set of requirements that any model should follow the concept. For example, see the page for the requirements of container
.
For example, for the requirements of the container
position, you should type the contents of container content as value_type
, and in the form of typedef iterators In the form of iterator
and const_iterator
In addition, you should define the start ()
and end ()
function as the returning etter, and so on.
Then you will need to change the function to work on any class instead of working on your vector
base class which adheres to the requirements imposed by the concept. It can be done again by the templateed tasks. You do not have to stick to the concepts used by STL, you can add your own self to the addition of the additional concepts defined in STL as a way of pasting the concepts. The advantage is that STL algorithms ( std :: sort
for example) can work on your containers.
Quick example:
square vector images {public: typed vector impressor iterator; Iterator start (); Iterator and (); }; Square vectorampll {public: typed vector importer Iterator; Iterator start (); Iterator and (); }; Template & lt; Typename vector concept & gt; Nothing to invade (Vector Conceptor and Container) {Vector Concept :: Iterator; This = container.bizin (); } Int main () {VectorImplA vecA; Vector Iamplab VCB; DoSomeOperations (vecA); // compiles! DoSomeOperations (vecB); // compiled as well! }
In the form of a bonus, to answer the basic question, consider the following design (although I will not go like this!):
Struct IteratorBase {virtual zero next () = 0; }; Structure IteratorA: IteratorBase {void next () {}; }; Structure IteratorB: IteratorBase {void next () {}; }; Class Iterator {IteratorBase * d_base; Public: Next zero () {d_base- & gt; next (); }};
Comments
Post a Comment