   C++ std library iterator inheritance HELP!
  This std code is simple enough to use as a template but when i tried to create a child class out of the iterator I'm getting all kinds of headaches, but I'm still prone to newbishness.I suppose I should explain my goal and someone might suggest a better method. With edited names for relevance, I have a class myContainer that contains a list<object>, and what I really want to add to this is a myContainerChild class to inherit the same functionality but operate on a list<childOfObject>. Unfortunately I can't use a virtual data member, because c++ just doesn't work like that, and I can't use a template because I need data specific to the object class and its children and as far as I know templates can't be that specific.I settled on leaving the list the same but adapting the add and remove functions to take only childOfObject even though the list remains a list<object>, as well as modifying the iterator to automatically cast the objects in the list as childOfObject, which should work as long as my add function is safe.I thought I finally had a good idea, but using the code below i quickly get the error ITERATOR LIST CORRUPTED! I suspect my constructor omits something important but the iterator code is so cryptic.// ->>    in sim.h// typedef std::list<sim *> simList;// typedef simList::iterator simListIt;// <<-class objectListIt: public simListIt{typedef simListIt Parent;public:objectListIt(simListIt input){this->_Mycont = input._Mycont;this->_Mynextiter = input._Mynextiter;this->_Ptr = input._Ptr;}object* operator->() const{// return pointer to class objectreturn (static_cast<object*>(&**this));}object& operator*() const{// return designated valuereturn (static_cast<object&>(*(reference)**(_Mybase_iter *)this));//return **this);}};If anyone has any suggestions, thanks in advance. My brain is running down trying to read through the std library and make sense of its internal workings. Every few lines there's a preprocessor command. Its kind of rediculous. By the way i'm using visual c++ 2008 express.   
  Just so you know, any identifier starting with an underscore followed by an upper case letter or starting with two underscores is reserved for the implementation.  In other words, the compiler gets to use them; you don't.  Of course the compiler doesn't necessarily stop you from using them should you choose to, but it is not standards compliant and not necessarily compatible with other compilers.But it gets worse.  You aren't supposed to derive from standard containers.  Just don't do it.  Honestly, you don't need to.Your casts and use of the * operator are horribly confusing.  There's a better way to do this (I'll suggest an alternative below).Your const functions do not return const references.  Someone could modify the object reference that you return, thus breaking your const promise.  Const correctness is a must read.I don't know why you say you can't use templates.  Why not just make a templated class where you pass in the type of the object you want to store in the list?  Something like this:template <typename T>class MyContainer{    public:   // you may have to use typename here too, I'm not sure   typedef std::list<T> ListType;   const ListType& getList() const   {  return myList;   }   ListType::iterator getListBegin()   {  return myList.begin();   }    private:   ListType myList;};// Later on in your codeMyContainer<object> myContainer;MyContainer<childOfObject> myContainerChild;Disclaimer: I haven't actually tested the code above.  It's just a way of showing things.  It may not be 100% corect (but it's close).   ;  @MikeTacular: actually, he's correct. operator* on pointers is a const function that can return a non-const reference. The following code is valid:int i    = 5;int * const p = &i*i = 6;@AnimateDream: list iterators are not classes or structures, so you can't inherit from them. They also have no such members as _MyCont, _Mynextiter or _Ptr. I know that your IDE or compiler might think that they do, and they might tell you so, and they might even let you use these, but never forget that you are progamming in C++. The most fundamental aspect of C++ is that "it works on my machine" is never a reliable indicator that your code is correct. If the language standard (or the stable parts of the official compiler documentation, if you don't care about portability) don't say it, then you shouldn't rely on it. Now, if I get what you're trying to do... I would make both myContainer and myContainerChild class templates, where myContainerChild<T> inherits from myContainer<T>. ;Quote:Original post by ToohrVyk@MikeTacular: actually, he's correct. operator* on pointers is a const function that can return a non-const reference. The following code is valid:int i = 5;int * const p = &i*i = 6;I'm not saying it's necessarily invalid, but bad practice.  I'm also not talking about making the address of the pointer/reference const, but the value the pointer/reference refers to const.  Perhaps I'm misunderstanding section 18.11 of the FAQ though, so if I am, please elaborate; I'd love to have a proper understanding.   ;Quote:Original post by MikeTacularPerhaps I'm misunderstanding section 18.11 of the FAQ though, so if I am, please elaborate; I'd love to have a proper understanding.The state of a given iterator is what value it references. The state of that value is not part of the state of the iterator, it is actually part of the container/sequence the iterator iterates over. This leads to four different combinations of const-ness : iterator : can change both iterator state and value stateconst iterator : can change value state but not iterator stateconst_iterator : can change iterator state but not value stateconst const_iterator : can change neither stateBut even assuming that const iterator wouldn't let you change the value state, you can always write:const iterator c_it;iterator it(c_it);modify(*it);That is, since the value state is the same for all iterators that reference that value, it's easy to create a non-const iterator to change the state through. ;  Thanks for the suggestions. I was aware of the problems I still had with const correctness, i was just having enough trouble getting it to allow me to compile.My apologies if my explanations have been difficult to understand. The problem with a template I tried to explain earlier, is that my class is really designed around a list of one specific class and its children, and uses its member data and functions. Here's the whole parent container class I created.//Basic storage hierarchy for everything in the simulation//A sim can be in only one simGroup//Becuase simGroup is a sim, it to can be stored in another simGroup//This should make it easy to organize game objects//::example:: bot4 might be stored in simGroup team2 which is in simGroup objects which is in root//  root->objects->team2->bot4typedef std::list<sim *> simList;typedef simList::iterator simListIt;class simGroup : public sim{public:typedef sim Parent;simList list;simGroup(){}//Add a sim to this simGroup, Ensure that sim is in only one simGroupvirtual bool add (sim* addition){if (addition->group)remove(addition);addition->group = this;list.push_back(addition);return true;}//remove a sim from whatever simGroup its invirtual bool remove(sim* removal){if (removal->group)removal->group->list.remove(removal);removal->group = NULL;return true;}bool isEmpty(){return list.empty();}//Update will update every sim in the simgroup.//Because simgroups are also sims, this is recursive.//Updating root should update everything in the simulation.virtual bool update(float deltaTime){//hge->System_Log((name + " " + typeid(*this).name() + "::update()").c_str());if (list.empty())return false;//sim* current = NULL;for(simList::iterator iter = list.begin(); iter != list.end(); iter++){(*iter)->update(deltaTime);}return true;}//Not used yetvirtual bool bigUpdate(){if (list.empty())return false;//sim* current = NULL;for(simList::iterator iter = list.begin(); iter != list.end(); iter++){(*iter)->bigUpdate();}return true;}};If you look at my simGroup::update() function, it calls the sim::update() on every item stored. If I were using a template, the items stored would be an undefined type with no member function update() to call. This class works great by itself but I want a child class of this container which 1. stores only a child class of the class its currently storing2. can safely use member functions of this child class that aren't a part of sim used in the list in the parent containerIts starting to look like the easiest way is just to not have my second container class inherit from the first, despite needing to perform all of the same operations.Quote:Original post by MikeTacularBut it gets worse. You aren't supposed to derive from standard containers. Just don't do it. Honestly, you don't need to.Technically I never did derive from a standard container I just derived from its iterator class. Which in retrospect is probably even worse.Quote:Original post by ToohrVyklist iterators are not classes or structures, so you can't inherit from them. They also have no such members as _MyCont, _Mynextiter or _Ptr. I know that your IDE or compiler might think that they do,Um. No actually it is a class in the standard library. Here's its definition header. Do a search for that.template <bool _SECURE_VALIDATION>class _Iterator: public _Const_iterator<_SECURE_VALIDATION>...and it also has all of the aforementioned data members. I can see the code and my ide isn't lying to me. I just didn't know if there were more data members I missed that might be critical to creating a copy of an initialized iterator.Quote:Original post by MikeTacular Just so you know, any identifier starting with an underscore followed by an upper case letter or starting with two underscores is reserved for the implementation.  Seems logical enough, and in retrospect given the difficulty i had I can see why they'd use that naming convention. I'll remember that for the future. Thanks.Quote:Original post by MikeTacularYour casts and use of the * operator are horribly confusing.  I agree the use of the * operator is horribly confusing. Its copied directly from the stl iterator class. Blame the people who wrote the standard library for that one. The casts themselves are the only new addition to those operator overrides, and they are the crucial part that was missing.Thanks for your support everyone.p.s. Why are none of my code tags working?   ;Quote:Original post by AnimateDreamUm. No actually it is a class in the standard library. Here's its definition header. Do a search for that.template <bool _SECURE_VALIDATION>class _Iterator: public _Const_iterator<_SECURE_VALIDATION>...and it also has all of the aforementioned data members. I can see the code and my ide isn't lying to me. I just didn't know if there were more data members I missed that might be critical to creating a copy of an initialized iterator.Names beginning with _ followed by an upper case are implementation details; they aren't part of the public interface of the standard library. This means that if you write code that depends directly on these types your code will only work with your current standard library implementation. It can and will break without warning if you move it to another compiler including possibly an upgraded version of your current compiler.   ;Quote:Original post by AnimateDreamIf you look at my simGroup::update() function, it calls the sim::update() on every item stored. If I were using a template, the items stored would be an undefined type with no member function update() to call.  Incorrect, the class will exist and be known when the template is instantiated.  Much like the difference in declaration and definition, Template definition and Instantiation are two different things.   ;Quote:Original post by AnimateDreamDo a search for that.> grep '_MyCont' `find /opt/local/solaris/include/c++/`Sorry, no results. Looks like my C++ compiler has no trace of the word _MyCont... nor does it define any _Iterator class, or contain any _SECURE_VALIDATION, or any of the terms you mention.The result would probably be the same with many of my other compilers, and I wouldn't be surprised if several versions of your compiler also didn't come with it. Quote:I can see the code and my ide isn't lying to me.O hai, welcome to C++. The IDE lies to you. The compiler lies to you. The entire C++ language is based on the assumption that the programmer knows what he is doing, so if you don't know what you're doing you will end up being screwed big time.In this particular situation, both the IDE and the compiler expect you to know that any identifiers starting with underscore-uppercase are reserved and should therefore not be relied on. So, the IDE doesn't bother hiding the _Iterator class from you because, since you are a competent C++ programmer, you know that a class with that name should not be used by your own code.;Quote:Original post by stonemetalQuote:Original post by AnimateDreamIf you look at my simGroup::update() function, it calls the sim::update() on every item stored. If I were using a template, the items stored would be an undefined type with no member function update() to call.  Incorrect, the class will exist and be known when the template is instantiated.  Much like the difference in declaration and definition, Template definition and Instantiation are two different things.Forgive me I didn't mean to refer to the instantiation of the simgroup class. I need to use the objects member functions and data in the containers member functions. Perhaps I'm missing something fundamental about templatesEDIT: Ok. Ok. I totally botched that short bit of code I just posted and as soon as I noticed it my internet connection went out so I'm just now retracting it. sorry.[Edited by - AnimateDream on May 13, 2009 1:09:23 PM]