c++ - Using Iterators to hide internal container and achieve generic operation over a base container -


I basically want to create a base container class which can return a normal errator to an example of the container Need to specify the class without iterator template, can be used for. I suppose I can not apply the base container to a class template, after which the template class needs traversal algorithms, which do not know in advance. The base container can be inherited and implemented within any (custom or standard) container type / implementation. There are some code samples to clear it:

  struct MyObject {int myInt; } // A Abstract Container Class Baccountainear {Public: Enter Virtual Zero MyObject (MyObject & obj) = 0; Virtual Iterator getFirst (); // Iterator type is for virtual display only for getLast (); // Iterator type is for demonstration purposes only) // sample container class uses a std :: vector example to manage the object class BaseContainer_Vector: Public Base Container {Public: Zero Insert MyObject (and MyObject & obj) ); Eg eg Just back vector itter (getFirst); // need to override the iterator? Iterator Mill Last (); // need to override the iterator? Private: std :: vector & lt; MyObject & gt; ObjectContainer; }  

I will have a list of container objects, and I want to store these two containers and objects.

  std :: vector & lt; MyContainer * & gt; Container; For (int i = 0; i  

I want to support the forward-promoting foreach macro statement. Unless the range_begin and range_end function are properly supported, but the example in the boostdock uses std :: string :: iterator as the return type, whereas what I want is a normal iterator class and I still can not understand how to do this.

  std :: vector & lt; MyContainer * & gt; Container; (Int i = 0; I & lt; containers. Size (); i ++) {BOOST_FOREACH (MyObject obj, * (container [i])) {std :: cout & lt; & Lt; Obj.myInt & lt; & Lt; Std :: endl; }}  

I think that I can define my own Iterator class, then each category that extends the basement operator, extending the basic repeater to its own character needed. However, I prefer to use standard eterrators (STLs or promotions) to support this framework, rather that writing my own etterators, I think this approach will work, but I have comments about its efficiency I am open to

Is there a practical approach that can solve this problem beautifully? Or am I missing a simple point which can solve this problem without any pain?

A similar question can be found, but the proposed solution seems a bit complicated for my needs, and the more I can understand the needs. As already mentioned, first of all, you need the meaning semantics because your artworks are usually copied around otherwise. Are there. Object will result in slicing.

  class BaseContainer {protected: class BaseIteratorImpl; // Simple class; Public for interface: Class Iterator {public: Itater (Const. Base interface); Encrypt; Private: Bassitatorimple * MIIMPL; }; Iterator start (); Iterator and (); }; // BaseContainer  

Then, BaseIterator next to all the methods m_impl .

In this way you get the value meaning Syntax with the polymorphic core.

Obviously, you have to handle deep-copied words and proper destruction.

Some notes:

  • Publish Errator and const_iterator
  • Change your methods to empty < / Code>, size , code for compatibility with STL algorithm> Start , start etc ...
< P> You can check for help about the concepts and operations that your operators should support for maximum compatibility.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -